Saturday, May 28, 2011

How to get previous page values in other web form in a .NET webapplication

Let us say in FirstPage.aspx we have some text assigned to the text box named serverNameText as shown below in the button1_click event.

public void button1_click(Sender s,object args e)
{
serverNameText="Hai";
}

In order to retrieve the below text in the next immediate webform (webform name is SecondPage.aspx) we can do it in 2 different ways.The following are the ways:

First way:
TextBox txt =(TextBox)Page.PreviousPage.FindControl("serverNameText");

Second way:
FirstPage.aspx: Response.Redirect("SecondPage.aspx?sName="+serverNameText);
SecondPage.aspx:string sName=Request.QueryString("sName");