Friday, 3 March 2017

ASP.Net Query String to pass data from one page (WebForm) to another page

In this article, we are going to learn how to pass data from one page (aspx page) to another page using Query String in ASP.Net. There are different types of technique available in ASP.Net to pass data from one webform to another webform. Below is the list.
1. Query String (We will discuss in this article)
2. Cross Page Postback (We will discuss in later article)
3. Context.Handler object We will discuss in later article)

Let's understand Query String with an example. First create two webforms i.e. WebForm1.aspx and WebForm2.aspx. In the first webform create two TextBox controls to capture First Name and Last Name and one Button control to send data from WebForm1.aspx to WebForm2.aspx. Below is the designer for first page.

<table>
   <tr>
      <td>FirstName</td>
      <td><asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox></td>
   </tr>
   <tr>
      <td>LastName</td>
      <td><asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></td>
   </tr>
   <tr>
      <td colspan="2">
       <asp:Button id="btnSend" runat="server" Text="Send Data" OnClick="btnSend_Click"/>
      </td>
   </tr>
</table>

Now, in the second webform create two Label controls to display First Name and Last Name. Below is the designer for second page.

<table>
   <tr>
      <td>FirstName: </td>
      <td><asp:Label ID="lblFirstName" runat="server"></asp:Label></td>
   </tr>
   <tr>
      <td>LastName: </td>
      <td><asp:Label ID="lblLastName" runat="server"></asp:Label></td>
   </tr>
</table>

Now, we will pass data from webform1 to webform2 onclick on Send Button. As you can see in below code, to pass data we need to append ?(question mark) after the destination url i.e. WebForm2.aspx and after ?(question mark) append key and its value. If you want to pass more than one key/value pair then use & (ampersand) as shown below.

// In webform1.aspx.cs
protected void btnSend_Click(object sender, EventArgs e)
{
   Response.Redirect("~/WebForm2.aspx?FirstName=" +txtFirstName.Text+ "&LastName=" + txtLastName.Text);
}

Now on click of submit button, data will be passed to WebForm2.aspx. To retrieve data from URL, use below code in WebForm2.

// In webform2.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
   lblFirstName.Text=Request.QueryString["FirstName"];
   lblLastName.Text = Request.QueryString["LastName"];

   // or you can specify index position
  // lblFirstName.Text = Request.QueryString[0];
  // lblLastName.Text = Request.QueryString[1];
}

You can see in below figure we have passed two key/value pair i.e FirstName and LastName. Data is passed trough the url.

Query String before passing data
Query String after passing data

In above example, we have passed FirstName as 'Sachin' and LastName as 'Tendulkar' and we were able to retrieve the values correctly. Now pass FirstName as 'Sachin & Rahul' and LastName as 'Tendulkar'. Here we got FirstName as 'Sachin' and LastName as 'Tendulkar' because we have used & (ampersand) in first TextBox. You can see in second image, url that formed is containing & (ampersand) two times. So FirstName will be retrieved as 'FirstName=Sachin%20' , here %20 is the space and LastName as 'LastName=Tendulkar' but our requirement is correctly retrieve first parameter as 'Sachin & Rahul'.

Query String with special symbol
Query String with special symbol output

To correct problem with &(ampersand), we can Encode the URL as shown below.

protected void btnSend_Click(object sender, EventArgs e)
{
    Response.Redirect("~/WebForm2.aspx?FirstName=" +
        Server.UrlEncode(txtFirstName.Text) + "&LastName=" + Server.UrlEncode(txtLastName.Text));
}

Now you can see in below figure. %20 is replaced by +(plus) and &(ampersand) is replaced by %26 using UrlEncode. Here we are able to retrieve values correctly.

Query String with url encode

We can use following code to retrieve the parameters.

protected void Page_Load(object sender, EventArgs e)
{
    for (int i = 0; i < Request.QueryString.Count; i++)
    {
        string Result = Request.QueryString[i];
    }
  
  // you can check null condition also
    if (!string.IsNullOrEmpty(Request.QueryString["FirstName"]))
    {
        lblFirstName.Text = Request.QueryString["FirstName"];
    }
}

More About Query Strings

1. Query strings are common way to send data from one page to another page.
2. Query strings are name/value collection pairs.
3. Query strings are appended to the page url.
4. ?(question mark), indicates the beginning of a query string and it's value.
5. It is possible to use more than one query string. The first query string is specified using the ?(question mark). Subsequent query strings can be appended to the URL using the &(ampersand) symbol.
6. To read the query string value, use Request object's QueryString property.

Disadvantages of Query Strings

1. There is a limit on the Query string length. Hence, Query strings cannot be used to send very long data.
2. Query strings are visible to the user, it should not be used to send sensitive information, unless it is encrypted.

Share:

0 comments:

Post a Comment

Email Subscription

Subscribe to our newsletter to get the latest articles directly into your inbox

Blog Archive

BUY FROM AMAZON