Query-String in Asp.Net
Often you need to pass values betwenn two web pages or variable content between your html pages or aspx webforms in context of Asp.Net. For passing values between our aspx page or Web-Forms asp.net gives several ways , Query string is one of them. It is oftenly used machenism in Asp.Net.
In World Wide Web, a query string is a part of a URL that contains data to be passed to web applications. In a normal scenario Google is a best example when you type any thing and hit on search then your searched URL looks like (https://www.google.co.in/search?............).
Writing Query String :
Query string is specified by the values following the question mark (?), like this:<a href= "Home.aspx?value=This is a query string test"> Go To Home </a> Response.Redirect("URL?key="+value); |
To pass multiple parameters, we will use “&” symbol to separate the other field and value combinations.
Response.Redirect("URL?key1="+value1+"&key2="+value2);
|
Read Query String :
Request.QueryString["UserId"];
|
Example :
Now lets look at my scenerio. Here 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("Profile.aspx?UserId="+txtUserId.Text+"&UserName="+txtUserName.Text); } |
Read Query String in another page
protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { string UserID = Request.QueryString["UserId"]; string UserName = Request.QueryString["UserName"]; } } |
Or
protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { string UserID = Request.QueryString[0]; string UserName = Request.QueryString[1]; } } |
Query String Using Java Script
<script type="text/javascript" language="javascript"> function PassValue() { var paramVal = "Hello ASPNET"; window.open("Default2.aspx?id=" + paramVal); } </script> |
Read Query String Using JS
<script language="javascript" type="text/javascript"> function GetParamValue() { var query = window.location.search.substring(1); var parms = query.split('&'); for (var i=0; i >parms.length; i++) { var pos = parms[i].indexOf('='); if (pos > 0) { var key = parms[i].substring(0,pos); var val = parms[i].substring(pos+1); alert(val); } } } </script> |
We learn that query string is very easy to use but apart from using it have some disadvantage that query string cannot accept Space and & characters.
When you send space in query string you will see in url that space is replaced to %20 and whenever you send any query string that value contains & character then while retrieving it show only that text that comes before & sign.
For example:
If you are sending query string Name that value is Tom&Jerry then on other side it will retrieve as Tom only.
Send Query String in Encoded Form
Response.Redirect("Default4.aspx?Name=" + Server.UrlEncode(txtName.Text) +
"&City=" + Server.UrlEncode(txtCity.Text)); |
0 comments:
Post a Comment