Home > DeveloperSection > Blogs > QueryString in asp.net

QueryString in asp.net


ASP.Net ASP.Net 
Ratings:
0 Comment(s)
 1382  View(s)
Rate this:

QueryString in asp.net

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:

<div>

        <table>

            <tr>

                <th style="text-align:left">Student ID</th>

                <th style="text-align:left">:</th>

                <td><asp:TextBox ID="txtID" runat="server"></asp:TextBox></td>

            </tr>

            <tr>

                <th style="text-align:left">Name</th>

                <th style="text-align:left">:</th>

                <td>

                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>

            </tr>

            <tr>

                <td colspan="2"></td>

                <td><asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_Click"/></td>

            </tr>

        </table>

    </div>

Step 2

Write the following code into button click:

protected void 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:Label ID="lblID" runat="server" Text=""></asp:Label>

        <br />

        Name: <asp:Label ID="lblName" runat="server" Text=""></asp:Label></b>

</div>

Step 4

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

protected void Page_Load(object sender, EventArgs e)

    {

        lblID.Text = Request.QueryString["ID"].ToString();

        lblName.Text = Request.QueryString["Name"].ToString();

    }

Step 5

Run the application:

After fill the click the send button


Don't want to miss updates? Please click the below button!

Follow MindStick