Introduction:
Here we will learn how to pass values from one page to another in asp.net using query string in C#, VB.NET with example or send values with query string in asp.net using C#, VB.NET or pass values between pages in asp.net using C#, VB.NET with example. By using query string parameters we can easily send values from one page to another page in asp.net based on our requirements.
Here we will learn how to pass values from one page to another in asp.net using query string in C#, VB.NET with example or send values with query string in asp.net using C#, VB.NET or pass values between pages in asp.net using C#, VB.NET with example. By using query string parameters we can easily send values from one page to another page in asp.net based on our requirements.
Description:
In Previous posts I explained asp.net query string with multiple parameters, asp.net encrypt or decrypt query string parameters, Get query string values in jQuery with example, jQuery get query string values with special characters, dynamically display meta tags in asp.net, differences between app settings 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 pass values from one page to another using query in asp.net using C# and VB.NET with example.
In Previous posts I explained asp.net query string with multiple parameters, asp.net encrypt or decrypt query string parameters, Get query string values in jQuery with example, jQuery get query string values with special characters, dynamically display meta tags in asp.net, differences between app settings 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 pass values from one page to another using query in asp.net using C# and VB.NET with example.
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. |
|||
|
|||
1 comments :
Hi, demo's are great but download would be good also.
Note: Only a member of this blog may post a comment.