How to access control’s value to another page (Pass Values between Web Pages in ASP.NET 2.0)


PostBack to another page in ASP.net 2.0 You can use PostBackUrl attribute of Button control of source page to post to another page. 

FirstPage.aspx:

<form id=”form1″ runat=”server”>
    <div>
<asp:Label ID=”Label1″ runat=”server” Text=”First Page” Width=”305px” ForeColor=”Navy”></asp:Label>
<
br />
<br />
<asp:Label ID=”Label2″ runat=”server” Text=”First Name” Width=”149px”></asp:Label>
<asp:TextBox ID=”txtFirstName” runat=”server”></asp:TextBox>
<br />
<asp:Label ID=”Label3″ runat=”server” Text=”Last Name” Width=”149px”></asp:Label>
<asp:TextBox ID=”txtLastName” runat=”server”></asp:TextBox>
<br /><br />                                     
<asp:Button ID=”BtnSubmit” runat=”server” PostBackUrl=”~/SecondPage.aspx” Text=”Go To Second Page” /></div></form>

SecondPage.aspx:

<form id=”form1″ runat=”server”>
<div>
<asp:Label ID=”Label1″ runat=”server” Text=”Second Page” Width=”305px” ForeColor=”Navy”></asp:Label>
  <br />
 <br />
<asp:Label ID=”Label2″ runat=”server” Text=”First Name” Width=”149px”></asp:Label>
<asp:Label ID=”LblFirstName” runat=”server” ForeColor=”#C00000″ Width=”157px”></asp:Label>
 <br />
<asp:Label ID=”Label4″ runat=”server” Text=”Last Name” Width=”149px”></asp:Label>
<asp:Label ID=”LblLastName” runat=”server” Text=”LblLastName” ForeColor=”#C00000″ Width=”155px”></asp:Label>
</
div>
</form>

SecondPage.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{   
// First we get a reference to the source page by
  
//using the target page’s PreviousPage property,
   
//this PreviousPage property holds the posted page.
   
//Now we check if this property(PreviousPage)is null or not.
if (Page.PreviousPage != null)
{    
// Now we call the FindControl method to get the control
TextBox FirstNameTextBox =
(TextBox)Page.PreviousPage.FindControl(“txtFirstName”);
TextBox LastNameTextBox =
(TextBox)Page.PreviousPage.FindControl(“txtLastName”);
// If it is not null then show the result.
if (FirstNameTextBox != null)
{        
LblFirstName.Text = FirstNameTextBox.Text;
 
}
        
if (LastNameTextBox != null)
      
{
        
LblLastName.Text = LastNameTextBox.Text;
      
}
   
}
 
}
For more detail reading please check:

http://msdn2.microsoft.com/en-us/library/6c3yckfw.asp

- Harpreet Virk

Leave a Reply