Wednesday, November 26, 2014

Explain Querystring in asp.net with example

Introduction: In this article today I am going to explain Querystring in asp.net with example

Description:

Querystring is a way to passing data from one page to another page through the URL. Data that is append to end of a page URL. We pass the information from one page and on another page can retrieve it.

Advantages:
1. It is supported by all browsers.
2. Querystring is easy to use.
3. Server resources are not required.
4. Extra afford are not needed to code.

Disadvantages:
1. All information is visible to user. Therefore, it is not secure.
2. There is a limit to URL length of 255 characters.

Example:


I have added two webforms to project. One page has textbox and button control. I want to pass the textbox enter value to next page on button click.

HTML markup of First page:
<fieldset style="width:400px">
    <legend>Querystring Example</legend>
    <table>
    <tr><td>Book Name:</td><td>  <asp:TextBox ID="txtname" runat="server"></asp:TextBox></td></tr>
    <tr><td></td><td><asp:Button ID="btnsend" runat="server" Text="Send" onclick="Button1_Click" /></td></tr>    
    </table>
    </fieldset>

On button click write the given code (C#):

protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Redirect("webform2.aspx?bname="+txtname.Text);   
    }

VB:
   Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles btnsend.Click
        Response.Redirect("webform2.aspx?bname=" + txtname.Text)

    End Sub
Html Markup of Second page where get the value:

   <table>
    <tr><td>Book Name:</td><td><asp:Label ID="lblbname" runat="server" Text=""></asp:Label></td></tr>
    <tr><td></td><td></td></tr>    
    </table>

Write the below given code to get the value (C#):

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblbname.Text = Request.QueryString["bname"];
        }
    }

VB:
  Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            lblbname.Text = Request.QueryString("bname").ToString()
        End If

    End Sub

Output:

Querystring in asp.net with example


Is this article helpful for you?

If yes post your comment to appreciate my work and fell free to contact me. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.

No comments:

Post a Comment