DEV Community

Janki Mehta
Janki Mehta

Posted on

Connecting Two Web Forms in ASP.NET

Connecting multiple web forms together is a common requirement in ASP.NET web applications. For example, you may want to pass data or values from one form to another after submitting the first form. There are a few different ways to accomplish this in ASP.NET.

Using Session State

One method is to use the ASP.NET session state to store values from the first form and then retrieve them on the second form. Here are the steps:

  • On the first web form (Form1.aspx), save any values you want to pass to the next form in the Session state. For example:
protected void Button1_Click(object sender, EventArgs e) 
{
  Session["UserName"] = TextBox1.Text;
  Session["Email"] = TextBox2.Text;
}
Enter fullscreen mode Exit fullscreen mode
  • After handling the postback on Form1, redirect to the second web form:
Response.Redirect("Form2.aspx");
Enter fullscreen mode Exit fullscreen mode
  • In the page load event on the second web form (Form2.aspx), retrieve the values from Session state:
protected void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
    string userName = Session["UserName"].ToString();
    string email = Session["Email"].ToString();

    //populate form controls with session values

  }
}
Enter fullscreen mode Exit fullscreen mode

It allows you to pass data between the two forms through session state.

Using Query String Parameters

Another approach is to pass data between forms using query string parameters.

  • On the first form, append the values you want to pass as query string parameters to the redirect URL:
Response.Redirect("Form2.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text);
Enter fullscreen mode Exit fullscreen mode
  • On the second form, retrieve the query string values in the page load event:
protected void Page_Load(object sender, EventArgs e)  
{

  if (!IsPostBack)
  {
    string name = Request.QueryString["name"];
    string email = Request.QueryString["email"];

    //populate controls 

  }

}
Enter fullscreen mode Exit fullscreen mode

The benefit of this approach is it avoids use of server-side state. The downside is the query string can be tampered with by the user.

Posting Form Data Via Cross-Page Posting

A third approach is to use cross-page posting to submit the form values from the first page directly to the second page. Here is how this works:

  • Set the PostBackUrl property on the submit button on Form1 to the URL of Form2:
<asp:Button ID="Button1" runat="server" Text="Submit" PostBackUrl="Form2.aspx" />
Enter fullscreen mode Exit fullscreen mode
  • On Form2.aspx, the posted form values from Form1 will be available in Request.Form in the page load event:
protected void Page_Load(object sender, EventArgs e)
{

  if (!IsPostBack)  
  {
    string name = Request.Form["TextBox1"];
    string email = Request.Form["TextBox2"];

    //populate controls

  }

}
Enter fullscreen mode Exit fullscreen mode

It submits the form values directly from one page to the other without redirects.

Top comments (0)