blog

Home / DeveloperSection / Blogs / QueryString in asp.net

QueryString in asp.net

Anchal Kesharwani3179 12-Aug-2014

In this blog, I’m explaining the concept of query string in asp.net.

In asp.net, the QueryString is used for passing the value one page to another page. The QueryString collection retrieves the values of the variables in the HTTP query string. The HTTP query string is specified by the values following the question mark (?). Several different processes can generate a query string. 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

For passing the variables asp.net gives the property QueryString of Request object.

Advantages of this approach

·    It is very easy.

Disadvantages of this approach

·    QueryString have a max length, if you have to send a lot information this approach does not work.

·    QueryString is visible in your address part of your browser so you should not use it with sensitive information.

·    QueryString cannot be used to send & and space characters.

Here give an example for query string whose send username and password from first page receive to another page.

Example
Step 1

First design the Form:

QueryString in asp.net

<div>
        <table>
            <tr>
                <thstyle="text-align:left">Student ID</th>
                <thstyle="text-align:left">:</th>
                <td><asp:TextBoxID="txtID"runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <thstyle="text-align:left">Name</th>
                <thstyle="text-align:left">:</th>
                <td>
                    <asp:TextBoxID="txtName"runat="server"></asp:TextBox></td>
            </tr>
            <tr>
                <tdcolspan="2"></td>
                <td><asp:ButtonID="btnSend"runat="server"Text="Send"OnClick="btnSend_Click"/></td>
            </tr>
        </table>
    </div>

Step 2

Write the following code into button click:

protectedvoid btnSend_Click(object sender, EventArgs e)
    {
        Response.Redirect("WebForm2.aspx?ID=" + txtID.Text + "&Name=" + txtName.Text);
    }
Step 3

Create WebForm2.aspx:

<div>
        <br/>
        <b>ID: <asp:LabelID="lblID"runat="server"Text=""></asp:Label>
        <br/>
        Name: <asp:LabelID="lblName"runat="server"Text=""></asp:Label></b>
</div>

Step 4

Write the following code in WebForm2.aspx at page load event:

protectedvoid Page_Load(object sender, EventArgs e)
    {
        lblID.Text = Request.QueryString["ID"].ToString();
        lblName.Text = Request.QueryString["Name"].ToString();
    }

Step 5

Run the application:

QueryString in asp.net

After fill the click the send button

QueryString in asp.net


Updated 18-Sep-2014

Leave Comment

Comments

Liked By