Home

Thursday, 14 July 2016

Send Data from one Page to another using Query String in c# Asp.net

Step 1) Create Form for user interaction-
WebForm1.aspx
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td>Name</td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Email</td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td colspan="2">
                <asp:Button runat ="server" ID="Botton1" Text="Send Data" OnClick="Botton1_Click"  />
            </td>
        </tr>  
    </table>
    </div>
    </form>
</body>
</html>
Step 2-Write Below code in WebForm1.aspx.cs on Button Click Event
 protected void Botton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/WebForm1.aspx?Name=" + Server.UrlEncode(TextBox1.Text) + "&Email=" + Server.UrlEncode(TextBox2.Text));
            //Response.Redirect("~/WebForm1.aspx?Name=" +TextBox1.Text.Replace("&","%26") + "&Email=" +TextBox2.Text.Replace("&","%26"));
        }
 Step 3-Write Below code in WebForm1.aspx where show query string data-
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table class="auto-style1">
            <tr>
                <td>Name</td>
                <td>
                    <asp:Label ID="Label1" runat="server"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    email</td>
                <td>
                    <asp:Label ID="Label2" runat="server"></asp:Label>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
Step 4- Add below code in WebForm2.aspx.cs PageLoad method
 protected void Page_Load(object sender, EventArgs e)
        {
           Label1.Text= Request.QueryString[0];
           Label2.Text = Request.QueryString[1];
        }
Step 5) Run application and enter field data after click on button we get below query string in URL-

http://localhost:2210/WebForm1.aspx?Name=pankaj&Email=pankaj%40gmail.com

No comments:

Post a Comment