且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

查找另一个页面的控件

更新时间:2023-01-07 14:44:54

1. set the PostBackUrl property of the Button to the New Page  


<asp:button id="Button1" runat="server" text="test now " postbackurl="~/Default5.aspx" xmlns:asp="#unknown" />





or you can use


<asp:Button ID="Button1" runat="server" Text="test now "

        onclick="Button1_Click2"   />





code behind 


protected void Button1_Click2(object sender, EventArgs e)
{
          Server.Transfer("Default5.aspx");
 
}





Note :         Response.Redirect("Default5.aspx"); will raise 
Error Object reference not set to an instance of an object.


in the the other page 





protected void Page_Load(object sender, EventArgs e)
   {
// this is page Default5
       if (!IsPostBack)
       {
           Button btn = ((Button)PreviousPage.FindControl("Button1"));
           Button1.Text = btn.Text;
       }

   }


如果您使用"Response.Redirect"或"PostBackUrl"重定向到当前页面,则下面的代码应该可以使用.但是,如果您以Page1.aspx开头并重定向到Page2.aspx,然后重定向到Page3.aspx,则找不到在Page1.aspx上放置的控件.您只能找到放置在Page2.aspx中的控件.

If you redirect to current page with "Response.Redirect" or "PostBackUrl" then below code should work. But If you starts with Page1.aspx and redirect to Page2.aspx and then redirect to Page3.aspx then you can not find control which placed on Page1.aspx. You can find control only placed with Page2.aspx.

RadioButton rb = (RadioButton)PreviousPage.FindControl("RadioBtn1");



如果有帮助,请投票.



Please vote if this helped you then.


如果两个页面是交叉页面,则可以获取控件的值.否则,您必须将控件的值存储在会话或其他会话中.
If the two page is cross page then you can get the value of the control. Otherwise you have to store the value of the control in session or others.