Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Query String in Asp.net with Example in C#, VB.NET

Oct 13, 2012
Introduction:

In this article I will explain how to create QueryString example in
asp.net using C# and VB.NET.

Description:

In Previous posts I explained
Dynamically display meta tags in asp.net, Differences between appsettings and connection strings, jQuery get hidden field values from asp.net gridview and many articles regarding Asp.net, Gridview, SQL Server, Ajax, JavaScript etc. Now I will explain how to create QueryString example in asp.net using C# and VB.NET.

Now I have one page which contains one textbox and button control I need to send textbox value to another page when we click on button control for that we need to write the code like this

protected void btnSend_Click(object sender, EventArgs e)
{
Response.Redirect("Default2.aspx?UserId="+txtUserId.Text);
}
Or

In case if we need to send multiple parameters to another page we need to write code like this

protected void btnSend_Click(object sender, EventArgs e)
{
Response.Redirect("Default2.aspx?UserId="+txtUserId.Text+"&UserName="+txtUserName.Text);
}
Now we need to get these values in another page (here I mentioned Default2.aspx) by using QueryString Parameter values with variable names or index values that would be like this


protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
lblUserId.Text = Request.QueryString["UserId"];
lblUserName.Text = Request.QueryString["UserName"];
}
}
Or


protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
lblUserId.Text = Request.QueryString[0];
lblUserName.Text = Request.QueryString[1];
}
}
If you want to see complete example first create new website and open Default.aspx page and write the following code


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>QueryString Example in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div><b>QueryString Example</b></div><br />
<div>
<table>
<tr>
<td><b>UserId:</b></td>
<td><asp:TextBox ID="txtUserId" runat="server"/></td>
</tr>
<tr>
<td><b>UserName:</b></td>
<td><asp:TextBox ID="txtUserName" runat="server"/></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnSend" Text="Send Values" runat="server" onclick="btnSend_Click"/></td>
</tr>
</table>
</div>
</form>
</body>
</html>
After that write the following code in code behind

protected void btnSend_Click(object sender, EventArgs e)
{
Response.Redirect("Default2.aspx?UserId="+txtUserId.Text+"&UserName="+txtUserName.Text);
}
Now Right click on your website and Add New item and add new page (Default2.aspx) open that Default2.aspx page and write the following code


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>QueryString Example in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div><b>QueryString parameter Values in Default2.aspx Page</b></div><br />
<div><b>UserId:</b><asp:Label ID="lblUserId" runat="server"/></div><br />
<div><b>UserName:</b><asp:Label ID="lblUserName" runat="server"/></div>
</form>
</body>
</html>
After that write the following code in code behind


protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
lblUserId.Text = Request.QueryString["UserId"];
lblUserName.Text = Request.QueryString["UserName"];
}
}
Demo


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

36 comments :

ramesh said...

Thank you. Excellent .

Anonymous said...

very very useful link...

Anonymous said...

nice helping alot......

Unknown said...

nice but i want to display the checkboxlist when we click on the button on same page.

safeer-ul-haq said...

sir plz make urlrewriting for the query string.
i want to look this url default2.aspx/suresh-dasari
default2/suresh-dasari plz provide some urlrewriter.net article

omer said...

Great article....really helpful

Anonymous said...

good

Kodexpression said...

thanks Sir but 1 question in my mind
How can i open popupwindow using Response.redirect().

Anonymous said...

thanks......... it has been very helpful...

srilekha said...

thanks sir it is useful for me. But i want to send that details to my mail. how can i send. please help me out

Anonymous said...

teri bhes ki aakh

Anonymous said...

Its not showing on my redirected second page
Home.aspx.cs
if (!IsPostBack)
{
lbl_displayuname.Text = Request.QueryString["uname"];
}

Login.aspx.cs
Session["UserName"] = uname.Text;
Response.Redirect("Home.aspx?uname"+Session["UserName"]);

Is there any mistake?
I really like your tutorials, They are very helpful

Anonymous said...

nice..

Vaishali Patel said...

Thanks... Great job by you. Very helpful

Anonymous said...

thanks.. it is so helpful

Help said...

MYCODE is

Page1:

Cno = GridView1.SelectedRow.Cells[6].Text;
Bno = GridView1.SelectedRow.Cells[3].Text;

Tno = GridView1.SelectedRow.Cells[7].Text;

Response.Redirect("~/RedirectionURL.aspx?BatchID=" + Bno + "&ClientID=" + Cno + "&TagId=" + Tno); this code i am writting in first page

Page 2: The page 1 values retrive values like this based on this values perforn operations finally the url Like this /RedirectionURL.aspx?BatchID=1&ClientID=101&TagId=200

Instead of that url, i want to /RedirectionURL.aspx?1.101.200 this url

int Bid = Convert.ToInt32(Request.QueryString.Get("BatchID"));
int Cid = Convert.ToInt32(Request.QueryString.Get("ClientID"));
string Tno = Request.QueryString.Get("TagId");

Unknown said...

Sir, can you please explain me how can i gives data dynamically to query string by label

Anonymous said...

Ur all articles are very good,easy way to understanding
So keep continue
Thanks

Anonymous said...

Your all articles are very useful..
Thank you so much for all your help

Unknown said...

fancy-box using jquery....then after windowpopup

Anonymous said...

For VB Part use this in Default2.aspx

Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
lblUserId.Text = Request.QueryString("UserId")
lblUserName.Text = Request.QueryString("UserName")
End If
End Sub

Unknown said...

nice...great work...thanks..

Anonymous said...

nice article

Athira Pillai said...

Thank you sir... really informative and simple..

Unknown said...

Here why we have used

if(!ispostback)
what does it mean?
if i will not write (!ispostback ) what will happen??
Thank you

Unknown said...

Thank you suresh... great work... nice example... Dont stop... keep Continue bro.

otr214422 said...
This comment has been removed by a blog administrator.
Anonymous said...

Nice work Brother
Regard's
Saad Saleem
From Pakistan

Anonymous said...

How to hide this query string in the address bar, which is security issues know...

In my application I need to hide the itemno(query string that i had passed) displaying in address bar should be hide
Eg:http://localhost64614/mysite/mypage?itemno=2201 tohttp://localhost64614/mysite/mypage
here I'm using master page concepts.. can U plz help me

parshotampd said...

hi suresh,
request you to also share prevent tampering with query string url.

i m looking for your valuable support

Anonymous said...

waah

Unknown said...

nice . very useful

Vijay Kumar Gajam said...

Hi,
how to pass values page to another page like
if i enter id number in search box then click search button result will display another page (id details will be display on another page) in asp.net C#, sql server.

please help me...!

arshad iqbal said...

Thanks , it was helpfull.

Anonymous said...

w4e5tyuk

Unknown said...

sir i need formula for to view some of the rows and columns in the table and also how to add some new data in the existing table.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.