Syed Raza
Syed Raza

Reputation: 149

How to redirect to another page and fire an event on that page?

I want to redirect to the another page in the same project and fire click event automatically. I am able to redirect to another page, but need help on how to fire the event automatically. The class from where i am redirecting is called Testing and the class to which i redirect isTabTest. My code in theTesting` class is:

  protected void LinkButton2_Click(object sender, EventArgs e)
  {
      Response.Redirect("TabTest.aspx");
  }

Upvotes: 4

Views: 4661

Answers (4)

Curtis
Curtis

Reputation: 103388

Rather than using a QueryString (which a user could manually type in themselves) I would recommend passing a Session variable like the following. I'm afraid my c# knowledge is non-existent, but here is a VB.NET equivalent:

FirstPage.aspx

Session("mySession") = "myValue"
Response.Redirect("TabTest.aspx?FireSession=1")

TabTest.aspx

If Request.QueryString("FireSession") = 1 AndAlso Session("mySession") IsNot Nothing AndAlso Session("mySession") = "myValue" Then
  RunMethod()
End If

Upvotes: 1

V4Vendetta
V4Vendetta

Reputation: 38220

You need to encapsulate the Chart display method into a separate method say like DisplayChart() and call up these method based on some QueryString parameter or just call it in the page_load of the TabTest.aspx

Avoid invoking a handler directly, Event handlers should only be invoked as a consequence of an event being fired, its better to call this method in the handler also.

Upvotes: 0

Anders Abel
Anders Abel

Reputation: 69270

You cannot directly fire the event on redirection.

A solution is to add a query string parameter to the redirect:

Response.Redirect("TabTest.aspx?ShowChart=true");

In the Page_Load event of TabTest.aspx, add the following code:

if(Request.QueryString["ShowChart"] != null 
  && Request.QueryString["ShowChart"] == "true"))
{
    // Call the click event handler for the button that shows the chart. Passing
    // `null, null` assumes that you don't use the sender or eventargs parameters
    // in the event handler.
    ShowChart_Click(null, null);
}

Upvotes: 3

Rob
Rob

Reputation: 164

Could you not fire the event in the page_load? you could stick it within a check on postback so it doesn't fire more than once?

Upvotes: 0

Related Questions