簡體   English   中英

如何重定向到另一個頁面並在該頁面上觸發事件?

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

我想重定向到同一項目中的另一個頁面並自動觸發click事件。 我可以重定向到另一個頁面,但需要有關如何自動觸發事件的幫助。 我重定向Testing and the class to which i redirect is稱為Testing and the class to which i redirect is TabTest . My code in the . My code in the Testing`類中的. My code in the是:

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

您無法直接在重定向上觸發事件。

解決方案是將查詢字符串參數添加到重定向:

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

在TabTest.aspx的Page_Load事件中,添加以下代碼:

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);
}

我建議傳遞一個Session變量,而不是使用QueryString(用戶可以手動輸入)。 我擔心我的c#知識不存在,但這是一個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

你能否在page_load中觸發事件? 你可以把它貼在回發的支票上,這樣它不會多次開火嗎?

您需要將Chart顯示方法封裝到一個單獨的方法DisplayChart()DisplayChart()並根據某些QueryString參數調用這些方法,或者只是在TabTest.aspx的page_load中調用它

避免直接調用處理程序,事件處理程序只應在事件被觸發時調用,最好在處理程序中調用此方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM