Wednesday, November 26, 2014

Explain Querystring in with example

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


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.

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.

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


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>
    <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>    

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

protected void Button1_Click(object sender, EventArgs e)

   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:

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

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"];

  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


Querystring in 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