How to redirect to another page and trigger an event on this page?

I want to redirect to another page in the same project and an automatic click event. I can redirect to another page, but you need help on how to fire the event automatically. The class I'm redirected from is called Testing and the class to which i redirect is TabTest . My code in the . My code in the Testing` class:

  protected void LinkButton2_Click(object sender, EventArgs e) { Response.Redirect("TabTest.aspx"); } 
+4
source share
4 answers

You cannot directly trigger an event when redirecting.

The solution is to add the query string parameter to the redirect:

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

In the Page_Load TabTest.aspx event, 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); } 
+3
source

Instead of using QueryString (which the user could manually enter themselves), I would recommend passing the Session variable, as shown below. I'm afraid my knowledge of C # does not exist, but here is the equivalent of VB.NET:

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 
+1
source

Could not fire event in page_load? can you insert it into the check on the reverse gear so that it does not work more than once?

0
source

You need to encapsulate the chart display method in a separate method, for example, as DisplayChart() , and call this method based on some QueryString parameter or just call it in page_load TabTest.aspx

Avoid direct access to the handler, event handlers should be called only because of the event that occurred, therefore it is better to also call this method in the handler.

0
source

All Articles