In this article I will explain with an example, how to redirect to another Page with QueryString parameters in ASP.Net using C# and VB.Net.
This article will also illustrate how to read (get) the values from QueryString parameters sent from a Page and display in ASP.Net with C# and VB.Net.
Redirecting to another Page with Parameters in ASP.Net
HTML Markup
The HTML Markup consists of an ASP.Net TextBox, a DropDownList and a Button.
Name:
<asp:TextBox ID = "txtName" runat="server" Text = "Jack Simone" />
<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" />
Code
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.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.aspx?name={0}&technology={1}", name, technology))
End Sub
Reading values from QueryString parameters and displaying in ASP.Net
HTML Markup
The HTML Markup consists of an ASP.Net Label.
<asp:Label ID="lblData" runat="server" />
Code
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 + " <b>Technology:</b> " + 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 &= "<b>Name:</b> " & name & " <b>Technology:</b> " & technology
lblData.Text = data
End If
End Sub
Conclusion
That’s all, it is only simple tutorial how to redirect to another page using QueryString. I hope you enjoy this article. Thank you