QueryString Usage in Asp.Net
Introduction:
In my previous article i explained Architecture of wpf, Asp.Net Ajax Tutorial,ViewState in Asp.Net Tutorial,Hidden Field in Asp.Net and various articles on WPF and Asp.Net and Ajax. Now we will see about QueryString Usage in Asp.Net i.e what is the purpose of Querystring, when to use Querystring in asp.net, what is the need of encoding and decoding QueryString in Asp.Net, Ways to pass values to different pages or same page for different requests,How to pass multiple values through QueryString, How to Build QueryString in Asp.Net.
Description:
QueryString:
It is one of the client - side statemanagement techniques in asp.net. It is one of technique to send variable values to another pages in Asp.Net through URL. Variable values are appended to URL while redirecting or Transfering to another webform or html page in Asp.Net.
For Sending values to another page we will use QueryString Property of Request Object. While searching on the net you may see URL's like
" http://www.sitename.com/welcome.aspx?firstname=aspdotnet&lastname=sekhar".
- In the above URL the information after ? symbol is called as QueryString.
- QueryString has two parts i.e Key and Value.
- We can send multiple values through QueryString Seperated By & Symbol.
- QueryString Collection is a NameValueCollection internally where we can access keys using by name or by index.
How to add QueryString Information to URL:
We know Response.Redirect("URL") is used to transfer current user to another page. We can use same to send user to another page along with QueryString Key- Values. All we have to do is to add QueryString information to URL and add this URL to Response.Redirect.
To append QueryString information to URL or to add multiple values to URL we will follow below approaches.
We can directly place the querystring information from controls to Url like below
Another way is using string.Format method to send multiple values through
Reading QueryString Values from URL:
In the target page we can read QueryString Values from QueryString Collection using Request.QueryString[] Property. We can read QueryString Values through key name or by index.
example:
To Read encoded QueryString use Server.UrlDecode()
Optimized way of reading QueryString:
It is better to check whether QueryString value is supplied or not before using QueryString Values using string.IsNullOrEmpty() method.
Need of Encoding and Decoding QueryString:
- URL of QueryString should contain only ASCII characters only. So if you are passing other than ASCII Characters then they need to be encoded.
- .Net Framework provides HttpServerUtility.UrlEncode and HttpServerUtility.UrlDecode methods for encoding and decoding Non - ASCII characters.
Advantages of QueryString:
- Light weight and not consume any server resource.
- Easy to use and Efficient Technique.
- Broad Support. Almost all browsers and devices supports this.
- Seo Friendly. where user can bookmark pages easily.
Disadvantages of QueryString:
- Limited Capacity: Most browsers and client - devices allow upto 255 characters limit on URL Length.
- We can pass information in string format only, to pass objects and large volumes of data we have to do lot more things.
- Security: Information send through QueryString will seen by Users and can be trapped.
Conclusion:
I hope you got atmost details about QueryString usage in Asp.Net, How to pass multiple values throgh querystring, How to append information to QueryString, Reading QueryString Values, Encoding and Decoding QueryString, Advantages and Disadvantages of QueryString etc.
Related Tutorials
- Architecture of WPF.
- Creation of Three Tier Architecture in .Net Application.
- Difference between App.config and Web.Config
- Asp.Net Validation Controls Tutorial
- Asp.Net Ajax Tutorials
- Difference between Machine.Config and Web.Config in Asp.Net Framework.
- Basic Drawings and Shapes in WPF.
- How to read and write key values from and to App.config Settings file .Net Windows Application.
- How to Use RangeValidator Control in Asp.Net with Example.
- Asp.Net Ajax Server Controls, Ajax Control Toolkit Download and Installation Tutorial.
- How to Use ViewState in Asp.Net(State Management Techniques).
- How to use Hidden Field in Asp.Net with Example(State Managemet Techniques).
- Beginners Tutorial on How to use QueryStrings in Asp.Net(State Management Techniques).
- Asp.Net Cookie Usage Overview with example(State Management Techniques).
- Access Specifiers or Modifiers in C# with example(Oops Concept).
Hi Sekhar please write article on hoe to include in blog posts syntax highlighter.
ReplyDeleteThanks
gekvprasad@studentrefer.blogspot.com