Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

QueryString in asp.net - Part 59

Suggested Videos
Part 56 - Cross page postback strongly typed reference
Part 57 - Opening new window using javascript in asp.net
Part 58 - Techniques to send data from one webform to another

Different techniques to move data from one webform to another 
1. Cross Page Postback: Discussed in Part 55 and Part 56
2. Context.Handler object - Discussed in Part 58
3. Query strings - Will be discussed in this session.

The following concepts will be discussed in the subsequent sessions
4. Cookies
5. Session state
6. Application state



Points to remember about query strings
1. Querystrings are name/value collection pairs
2. Using querystrings, is a very comman way to send data from one webform to another.
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. There is a limit on the Query string length. Hence, Query strings cannot be used to send very long data.
7. Query strings are visible to the user, hence should not be used to send sensitive information, unless encrypted.
8. To read the query string value, use Request object's QueryString property.
9. &(ampersand) is used to concatenate query strings, so if you want to send &, as value for the query string there are 2 ways, as shown below
Using Server.UrlEncode() method
Response.Redirect("WebForm2.aspx?UserName=" + Server.UrlEncode(txtName.Text) + 
    "&UserEmail=" + Server.UrlEncode(txtEmail.Text));

Or
&(ampersand) is encoded as %26, so use, Replace() function to replace & with %26
Response.Redirect("WebForm2.aspx?UserName=" + txtName.Text.Replace("&", "%26") +
    "&UserEmail=" + txtEmail.Text.Replace("&", "%26"));



WebForm1.aspx HTML: We want to send Name and Email, that user enters on WebForm1.aspx to WebForm2.aspx using query strings.
<div style="font-family: Arial">
<table>
    <tr>
        <td colspan="2">
            <h1>
                This is WebForm1</h1>
        </td>
    </tr>
    <tr>
        <td>
            <b>Name</b>
        </td>
        <td>
            :<asp:TextBox ID="txtName" runat="server">
            </asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            <b>Email</b>
        </td>
        <td>
            :<asp:TextBox ID="txtEmail" runat="server">
            </asp:TextBox>
        </td>
    </tr>
    <tr>
        <td colspan="2"> 
            <asp:Button ID="btnSendData" runat="server" 
            Text="Go to WebForm2" onclick="btnSendData_Click" />
        </td>
    </tr>
</table>
</div>

WebForm1.aspx.cs
protected void btnSendData_Click(object sender, EventArgs e)
{
    //Using Server.UrlEncode to encode &(ampersand)
    //Response.Redirect("WebForm2.aspx?UserName=" + Server.UrlEncode(txtName.Text) + 
    //    "&UserEmail=" + Server.UrlEncode(txtEmail.Text));
            
    //Using String.Replace() function to replace &(ampersand) with %26 
    Response.Redirect("WebForm2.aspx?UserName=" + txtName.Text.Replace("&", "%26") +
        "&UserEmail=" + txtEmail.Text.Replace("&", "%26"));
}

WebForm2.aspx HTML:
<div style="font-family: Arial">
<table>
    <tr>
        <td colspan="2">
            <h1>This is WebForm2</h1>
        </td>
    </tr>
    <tr>
        <td>
            <b>Name</b>
        </td>
        <td>
            :<asp:Label ID="lblName" runat="server">
            </asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>Email</b>
        </td>
        <td>
            :<asp:Label ID="lblEmail" runat="server">
            </asp:Label>
        </td>
    </tr>
</table>
</div>

WebForm2.aspx.cs Code:
protected void Page_Load(object sender, EventArgs e)
{
    // Read the QueryString values 
    lblName.Text = Request.QueryString["UserName"];
    lblEmail.Text = Request.QueryString["UserEmail"];
}

1 comment:

  1. Hi Venkat,
    I would like to express my opinion about the url encoding. I think it's better to use the Server.URLEncode function, because without it we should write the "replace" function for each specific string that is not transferred as is but that must be encoded within the QueryString. I mean, if I want to transfer properly not only the "&" character but also, for example, the string "%26" I have to add another specific "replace". And I have to be very careful in which order execute these replace functions. Because if the field to transfer contains both: the character "&" and the string "%26", it's necessary first replace the string "%26" with "%2526" and then the "&" character with "%26". Otherwise as the result we will have two strings "%26" transferred.
    So, imho, it's better to use always the Server.URLEncode function which will ensure the correct encoding.
    Thanks for all the effort you put into your lessons!

    ReplyDelete

It would be great if you can help share these free resources