Interview Questions on Query Strings in ASP.NET


Give an example of using querystrings to send data from one page to another?
Query strings are a very simple and popular technique to pass data from one Web page to the next. You send data as part of the URL. In the below example FName and LName are sent as part of the URL. In the page load of QueryStrings2.aspx we use Request.QueryString to read the values. As we are sending more than one query string we use the & symbol to seperate query strings.

//Code to send query strings FName and LName as part of the URL
QueryStrings2.aspx?FName=David&LName=Boon

protected void Page_Load(object sender, EventArgs e)
{
//Code to read Query String values
string FirstName = Request.QueryString["FName"];
string LastName = Request.QueryString["LName"];
Response.Write("Data from QueryStrings1.aspx : " + FirstName + ", " + LastName);
}


Give an example to send Query Strings from code?
You can send query strings from server side code using the Response.Redirect() method as shown below.
Response.Redirect("QueryStrings2.aspx?FName=David&LName=Boon");

What are the advantages of using Query Strings?
1.
Query strings are easy to implement.
2. Browser support for passing values in a query string is nearly universal.
3. Query strings are contained in the HTTP request for a specific URL and do not require server resources.

What are the disadvantages of using querystrings to send data from one page to another?
1.
Query strings are insecure because the information in the query string is directly visible to the user on the address line in the browser.
2. Many browsers impose a 255 URL character limit which can limit their flexibility.

8 comments:

  1. Response.Redirect("QueryStrings2.aspx?FName=server.urlencode(David&Siva)");

    ....URL encoding ensures that all browsers will correctly transmit text in URL strings. Characters such as ?, &,/, and spaces..

    ~Sivaprakasam

    ReplyDelete
    Replies
    1. Leaving this note here for my own sake. How to urlencode a querystring parameter: Server.UrlEncode(value).

      Delete
  2. Can i pass QueryString using Server.Transfer() Methode?

    ReplyDelete
    Replies
    1. Server.Transfer method doesn't update the destination page url so query string wont be visible...

      Delete
    2. Server.Transfer method doesn't update the destination page url so query string wont be visible...

      Delete
  3. you cannot pass Querystring using Server. Transfer()Method

    ReplyDelete
  4. QueryStrings2.aspx?FName:David&LName:Boon

    if we use ' : ' instead of ' = ' in above statement , in control its doesn't retaining its state .

    Why its happening ?

    ReplyDelete
    Replies
    1. Which scenario use the query string is best practices ?

      Delete

If you are aware of any other asp.net questions asked in an interview, please post them below. If you find anything missing or wrong, please feel free to correct by submitting the form below.

 
Disclaimer - Terms of use - Contact Us