views:

3649

answers:

7

If I have a button inside one page for example "main.aspx". And upon being clicked I want the clicked method in "main.aspx.cs" to call a function from a different *.aspx.cs page but also redirect to that *.aspx page...is that possible?

A: 

Why don't you just redirect with a querystring parameter and then execute the function on the destination page?

Nebakanezer
That is what I had originally, onpage load it would look to see if a querystring was entered and call a function if it was...problem was that if I recalled the function, on pageload it would get called twice because it would not erase the querystring
is there a way to reset the querystring without reloading the page?
everything I try errors out and says the collection is read only. This is what I am trying, Request.Form.Clear();
You cannot change the Request.Form... just Redirect back to the same page with an updated URL.
Bryan
A: 

Please check this thread in asp.net forum.

Shoban
+2  A: 

You need to understand the ASP.NET Page lifecycle and you'll see why this is not Kosher. If you really need to call a method from multiple pages, it sounds like a good candidate for some external class/object.

Alternately, instead of using a querystring parameter, you can set a Session flag and then redirect to this new page. Then clear the session flag when you call that page's method.

Bryan
+1 for indicating it's not kosher. -1 for using session to communicate actions
Robert Paulson
Bah, I suppose you never use global variables either? :)
Bryan
+2  A: 

A more concrete example of what you're trying to do would be useful. Otherwise you'll get all sorts of answers, many of which will be off the mark.

You should put common code in the App_Code folder. You should also not have any business logic inside a forms code-behind.

The fact that you need one page to call a method in another page indicates that you haven't done this. Pages are for displaying and interpreting actions, but they should not hold any of the business logic.

e.g. a lame example of a shopping cart.

    ProductView.aspx - calls Cart.AddToCart(Product p)
    CartView.aspx - displays all items in the cart, and the user
                    can update or remove items.
        - calls Cart.RemoveItem(int cartIndex)
        - calls Cart.UpdateItem(int cartIndex, int newItemCount)

Cart itself doesn't belong in either CartView.aspx or ProductView.aspx. It instead belongs in ~/App_Code/Cart.cs

So your solution could look something like

/ (root of your web folder)
 Product/
          ProductView.aspx
          ProductView.aspx.cs

 Cart/
          CartView.aspx
          CartView.aspx.cs

 App_Code/
          Cart.cs 
          Product.cs

Also, to add the App_Code folder if it's not already there, right-click the web project and select Add ASP.NET folder and choose App_Code

Robert Paulson
That makes sense. Are the functions inside Cart.cs only accessible to CartView.aspx or are accessible to any *.aspx page?
Anything in ~/App_Code is available to all pages.
Robert Paulson
A: 

In a very basic way, for Main.aspx to go to Other.aspx and to pass some small amount of data on the querystring to indicate an action.

Main.aspx

protected void OnSomeButtonClicked(object sender, EventArgs e)
{
  if( someCondition )
  {
    Response.Redirect("~/Other.aspx?action=runAway");
  }
}

in Other.aspx

protected void Page_Load(object sender, EventArgs e)
{
  if( !IsPostBack )
  {
    if( "runAway".Equals(Request.QueryString["action"] )
    {
      RunAway();
    }
  }
}

You should also read up on Server.Transfer versus Response.Redirect. Which one to use is situation dependent.

Also note that QueryString parameters can be altered easily by the user, so always verify them and never trust the user.

Robert Paulson
A: 

Instantiate an object from your other page class and call the method.

protected Button1_Clicked(Sender s, Eventargs e){
        MyNamespace.Page_Other po = new MyNamespace.Page_Other();
        po.Method1();
        Response.Redirect("~/page_other.aspx");
}
Philippe
A: 

HI I have a aspx page with a link . If i click that link i want the page to be redirected to another aspx page and execute the java script in it .. ...

 plz some one help ... .. 

Thanks .. .. .

jazz