Pass variable from one page to other in Asp.net

Satyapriyanayak
Posted by Satyapriyanayak under ASP.NET category on | Points: 40 | Views : 1030
We will learn how to pass values or variables from one page to other page.

FirstForm.aspx code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="FirstForm.aspx.vb" Inherits="FirstForm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Font-Bold="True" Text="First Name"
Width="160px"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="200px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Name Required"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="Label2" runat="server" Font-Bold="True" Text="Email"
Width="160px"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" Width="200px"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="TextBox2" ErrorMessage="Invalid Email"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" ErrorMessage="Email Required"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="Label3" runat="server" Font-Bold="True" Text="Mobile No"
Width="160px"></asp:Label>
<asp:TextBox ID="TextBox3" runat="server" Width="200px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox3" ErrorMessage="Mobile No. Required"></asp:RequiredFieldValidator>
<br />
<asp:Label ID="Label4" runat="server" Font-Bold="True" Text="Country"
Width="160px"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>India</asp:ListItem>
<asp:ListItem>usa</asp:ListItem>
<asp:ListItem>uk</asp:ListItem>
<asp:ListItem>Nepal</asp:ListItem>
<asp:ListItem>Srilanka</asp:ListItem>
<asp:ListItem>Australia</asp:ListItem>
<asp:ListItem>Germany</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="DropDownList1" ErrorMessage="Country Required"></asp:RequiredFieldValidator>
</div>
<asp:Button ID="Button1" runat="server" Font-Bold="True"
onclick="Button1_Click" Text="Pass variables to second form" />
</form>
</body>
</html>

FirstForm.aspx.vb code

Partial Class FirstForm
Inherits System.Web.UI.Page
Dim url As String
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
url = "SecondForm.aspx?name=" & TextBox1.Text & "&email=" & TextBox2.Text & "&mobileno=" & TextBox3.Text & " &country=" & DropDownList1.SelectedItem.Text
Response.Redirect(url)
End Sub
End Class

SecondForm.aspx code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="SecondForm.aspx.vb" Inherits="SecondForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Font-Bold="True" Text=" Email:*"
Width="160px"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="200px"></asp:TextBox>
<br />

<asp:Label ID="Label4" runat="server" Font-Bold="True" Text="First Name:*"
Width="160px"></asp:Label>
<asp:TextBox ID="TextBox4" runat="server" Width="200px"></asp:TextBox>
<br />

<asp:Label ID="Label6" runat="server" Font-Bold="True" Text="Mobile Number: *"
Width="160px"></asp:Label>
<asp:TextBox ID="TextBox6" runat="server" Width="200px"></asp:TextBox>
<br />
<asp:Label ID="Label7" runat="server" Font-Bold="True"
Text="You have selected: *" Width="160px"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<br />
</div>
<br />
</form>
</body>
</html>

SecondForm.aspx.vb code

Partial Class SecondForm
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox4.Text = Request.QueryString("name")
TextBox1.Text = Request.QueryString("email")
TextBox6.Text = Request.QueryString("mobileno")
DropDownList1.Items.Add(Request.QueryString("country"))
End Sub
End Class

Comments or Responses

Login to post response