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.开发者_StackOverflow 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 is
TabTest. My code in the
Testing` class is:
protected void LinkButton2_Click(object sender, EventArgs e)
{
Response.Redirect("TabTest.aspx");
}
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);
}
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
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?
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.
精彩评论