ARTICLE

Pass variable from one page to other using VB.NET

Posted by Satyapriya Nayak Articles | ASP.NET using VB.NET August 17, 2011
In this article we will learn how to pass values or variables from one page to other page.
Reader Level:

In this article 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

Output

qstring1.gif

qstring2.gif

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor