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.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay