Several time in ASP.NET applications we need to transfer data or information provided by user from one aspx page to another. To transfer data we can use several method, QueryString is one of them.
Query strings are data that is appended to the end of a page URL. They are commonly used to hold data like page numbers or search terms or other data that isn't confidential. Unlike ViewState and hidden fields, the user can see the values which the query string holds without using special operations like View Source.
QueryString is an example of GET method. QueryString is very handy when we need to pass low secure small content from one page to another page. But if we need to pass secure information like credit card number etc or large amount of data then we can't use QueryString.
Structure of Query String
Query strings are appended to the end of a URL. First a question mark is appended to the URL's end and then every parameter that we want to hold in the query string. The parameters declare the parameter name followed by = symbol which followed by the data to hold. Every parameter is separated with the ampersand symbol.
You should always use the HttpUtility.UrlEncode method on the data itself before appending it.
Here are example of how we can use QueryString across pages.
QueryString example [C#]
Pass the QueryString to another page as:
http://www.abc.com?Default.aspx?fname=Sandeep
Here we have passed fname as QueryString to Default.aspx page. Now we'll access QueryString's value on page's code behind(In Default.aspx.cs) as:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["fname"] != null)
{
String firstName = Request.QueryString["fname"];
Response.Write("First Name is " + firstName);
}
}
}
Result:
First Name is Sandeep
Two query string parameters
Now we have familiar with QueryString, Here we see how you can test two query string URL parameters. Another common requirement is to test two different query strings. You may have to use either one or both at once.Pass the two QueryString as:
http://www.abc.com?Default.aspx?fname=Sandeep&lname=Tada
In above line we pass two querystring with &, we can pass more querystring like this if we required.
?fname=Sandeep&lname=Tada
Now we'll access QueryString's value on page's code behind(In Default.aspx.cs) as:
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["fname"] != null && Request.QueryString["lname"] != null)
{
String firstName = Request.QueryString["fname"];
String lastName = Request.QueryString["lname"];
Response.Write("Name is " + firstName + " " + lastName);
}
}
}
Result:
Name is Sandeep Tada
Query String LimitationsYou can use query string technique when passing from one page to another but that is all. If the first page need to pass non secure data(as mentioned above) to the other page it can build a URL with a query string and then redirect. You should always keep in mind that a query string isn't secure and therefore always validate the data you received. There are a few browser limitation when using query strings. For example, there are browsers that impose a length limitation on the query string.Another limitation is that query strings are passed only in HTTP GET command.
Maximum length of Querystring in ASP.NETMaximum length of Querystring is based on the browser not depend upon the asp.net. here is some informationMaximum length of a querystring in IE 4.0 and above is ~2048 characters
IE. 4,5,6,7, - 2,048 characters.
Opera supports - 4050 characters.
Netscape 6 supports - 2000 characters.
Firefox Supports - 6000 characters
Cheers!!!! हैप्पी प्रोग्रम्मिंग :)
Comments
Post a Comment