In this article I will explain with an example, how to pass (send) value from one Page to another using
QueryString in
ASP.Net with C# and VB.Net.
This article will also illustrate how to read values from
QueryString sent from a Page and display in
ASP.Net with C# and VB.Net.
HTML Markup
The following
HTML Markup consists of:
TextBox – For capturing user input.
DropDownList - For selecting values.
Button – For send selected values as QueryString parameter.
The Button has been assigned with OnClick event handler.
Name:
<asp:TextBox ID="txtName" runat="server" Text="Mudassar Khan" />
<br />
<br />
Technology:
<asp:DropDownList ID="ddlTechnology" runat="server">
<asp:ListItem Text="ASP.Net" Value="ASP.Net" />
<asp:ListItem Text="PHP" Value="PHP" />
<asp:ListItem Text="JSP" Value="JSP" />
</asp:DropDownList>
<asp:Button ID="btnSend" Text="Send" runat="server" OnClick="Send" />
Passing (sending) values from one Page to another using QueryString in ASP.Net
When the
Send Button is clicked, the
Name and
Technology values are fetched from the
TextBox and
DropDownList controls and are set in the URL as
QueryString parameters and the Page is redirected.
C#
protected void Send(object sender, EventArgs e)
{
string name = txtName.Text;
string technology = ddlTechnology.SelectedItem.Value;
Response.Redirect(string.Format("~/Page2_CS.aspx?name={0}&technology={1}", name, technology));
}
VB.Net
Protected Sub Send(sender As Object, e As System.EventArgs)
Dim name As String = txtName.Text
Dim technology As String = ddlTechnology.SelectedItem.Value
Response.Redirect(String.Format("~/Page2_VB.aspx?name={0}&technology={1}", name, technology))
End Sub
HTML Markup
The following HTML Markup consists of:
<asp:Label ID="lblData" runat="server" />
Reading values sent using QueryString and displaying in ASP.Net
Inside the
Page Load event, the
Name and
Technology values are fetched from the
QueryString and displayed in
Label control.
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string name = Request.QueryString["name"];
string technology = Request.QueryString["technology"];
string data = "<u>Values from QueryString</u><br /><br />";
data += "<b>Name:</b> " + name + " Technology: " + technology;
lblData.Text = data;
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim name As String = Request.QueryString("name")
Dim technology As String = Request.QueryString("technology")
Dim data As String = "<u>Values from QueryString</u><br /><br />"
data &= "Name: " & name &" <b>Technology:</b> " & technology
lblData.Text = data
End If
End Sub
Screenshot
Demo
Downloads