QueryString
When we need to pass variables content from one aspx or html page to another page through URL we use QueryString.
There several choice to pass variables from one page to another but in this session we use QueryString.
QueryString use to display different data on the same page.
Syntax of QueryString
Request.QueryString(variable)[(index).count]. e.g. http://localhost:50419/z-test2.aspx?UserName=Mark&EmailId=Mark@gmail.comNote:-
1. QueryString consist of two parts (field and value), and each of pair separated by ampersand (&) or semicolon(‘;’). i.e & or ; is used between variables.
2. ? (QuestionMark) sign, indicates the start of a QueryString and & indicate start of another QueryString.
Example
First we create new website "AspDotNets" (you can keep any name) and add "z-test.aspx" webform in website and then write the following code in source mode (look at bottom in webform three mode show design, split and source).
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="z-test.aspx.cs" Inherits="AspDotNets.z_test" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> Name:<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br /> Email:<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox><br /> <asp:Button ID="btnSubmit" runat="server" Text="go to Another Page" OnClick="btnSubmit_Click"/><br /> </form> </body> </html>
Now double click on button in design mode and paste the following code in code behind of z-test.aspx i.e. in z-test.aspx.cs
using System; namespace AspDotNets { public partial class z_test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click(object sender, EventArgs e) //carefully copy paste the code because "btnSubmit_Click" should must match with onclick event of button in aspx page i.e. OnClick="btnSubmit_Click" { Response.Redirect("~/z-test2.aspx?UserName=" + txtName.Text + "&EmailId=" + txtEmail.Text); } } }Now For receiving the value from the z-test.aspx, we add “z-test2.aspx” webform.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="z-test2.aspx.cs" Inherits="AspDotNets.z_test2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> Name:-<asp:Label ID="lblName" runat="server"></asp:Label><br /> EmailId:-<asp:Label ID="lblEmail" runat="server"></asp:Label><br /> </form> </body> </html>Now write the following code in behind (go to code behind by right click on aspx then select View Code).
using System; namespace AspDotNets { public partial class z_test2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { lblName.Text = Request.QueryString["UserName"]; // We can also retrieve value using their position so we can write code like Request.QueryString[0] also lblEmail.Text = Request.QueryString["EmailId"]; // Request.QueryString[1] } } }Now run the “z-test.aspx” by right click view in browser. Now, enter "Mark" in name textbox and mark@gmail.com in email textbox.
Enter Value in textbox |
Show transfer data from one page to another using QueryString through url |
Note:-
If we enter "Mark & Tony" in Name textbox and Email textbox "mark@gmail.com" than outPut Will be like Name:-Mark and Email:- mark@gmail.com.
Transfer data from one page to another using QueryString through url |
After click on button
Show transfer data from one page to another using QueryString through url |
If we use code like below.
lblName.Text = Request.QueryString[0]; lblEmail.Text = Request.QueryString[1];0 receive first QueryString and 1 receive second QueryString and as we know QueryString separate by & sign.
If retrieve value using their position and we enter we enter "Mark" in Name textbox and in Email textbox enter "mark@gmail.com" than outPut Will be like Name:-Mark Emial:-mark@gmail.com.
But if we enter "Mark & Tony" in name textbox and mark@gmail.com in Email textbox than outPut Will be like Name:-Mark, Emial:-Tony because & use as separator for second QueryString. see in example below.
Example
code at z_test.aspx.cs
code at z_test.aspx.cs using System; namespace AspDotNets { public partial class z_test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click(object sender, EventArgs e) { Response.Redirect("~/z-test2.aspx?UserName=" + txtName.Text + "&EmailId=" + txtEmail.Text); } } }code at z_test2.aspx.cs
using System; namespace AspDotNets { public partial class z_test2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { lblName.Text = Request.QueryString[0]; lblEmail.Text = Request.QueryString[1]; } } }Now run program, and enter "Mark & Tony" in Name textbox and click on button.
Show transfer data from one page to another using QueryString through url |
use & as value in QueryString
If we want to use & as value in name, not as separator for second QueryString, we can achieve this by two way.1. using UrlEncode
2. By Repalce & and space
Example
1. using UrlEncode
code at z_test.aspx.cs
using System; namespace AspDotNets { public partial class z_test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click(object sender, EventArgs e) { Response.Redirect("~/z-test2.aspx?UserName=" + Server.UrlEncode(txtName.Text) + "&EmailId=" + Server.UrlEncode (txtEmail.Text)); //UrlEncode Encode the value which is in the url. } } }code at z_test2.aspx.cs
using System; namespace AspDotNets { public partial class z_test2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { lblName.Text = Request.QueryString[0]; //or lblName.Text = Request.QueryString["UserName"]; lblEmail.Text = Request.QueryString[1]; //or lblEmail.Text = Request.QueryString["EmailId"]; } } }Now run the programe and Enter Mark & Tony in Name textbox and enter mark@gmail.com in EmailId textbox and click on button.
Transfer data from one page to another using QueryString through url |
Show transfer data from one page to another using QueryString through url |
2. By Repalce & and space
z_test.aspx.cs
using System; namespace AspDotNets { public partial class z_test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click(object sender, EventArgs e) { Response.Redirect("~/z-test2.aspx?UserName=" + txtName.Text.Replace("&","%26") + "&EmailId=" + txtEmail.Text.Replace ("&","%26")); } } }code at z_test2.aspx.cs
using System; namespace AspDotNets { public partial class z_test2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { lblName.Text = Request.QueryString[0]; //or lblName.Text = Request.QueryString["UserName"]; lblEmail.Text = Request.QueryString[1]; //or lblEmail.Text = Request.QueryString["EmailId"]; } } }Now run the program and enter "Mark & Tony" in name textbox and enter mark@gmail.com in email and click on button.
Show transfer data from one page to another using QueryString through url |
Note:-
In URL space is replace by %20 and & is replace by %26.
0 comments:
Post a Comment