Tuesday, 7 March 2017

Asp.Net Using Query String to Send Values from One Page to Another




Hi,I am Navid Shaikh today i am going to show you how to send (pass) Value one page to another page using Query String   in Asp.net C#

Now I have one page which contains one textbox and button control I need to send textbox value to another page when we click on button control for that we need to write the code like this





 protected void btnSend_Click(object sender, EventArgs e) {

   Response.Redirect("Default2.aspx?UserName="+txtUserName.Text+"&Password="+txtPassword.Text);

      }

If you want to see complete example first create new website and open Default.aspx page and write the following code


<body>
<form id="form1" runat="server">
<div>
<b>QueryString Example</b></div>
<br />
<div>
<table>
<tr>
<td><b>Username:</b></td>
<td><asp:textbox id="txtUserName" runat="server"></asp:textbox></td>
</tr>
<tr>
<td><b>Password:</b></td>
<td><asp:textbox id="txtPassword" runat="server"></asp:textbox></td>
</tr>
<tr>
<td></td>
<td><asp:button id="btnSend" onclick="btnSend_Click" runat="server" text="Send Values"></asp:button></td>
</tr>
</table>
</div>
</form>
</body>




After that add above c# code in button click event.

Now Right click on your website and Add New item and add new page (Default2.aspx) open that Default2.aspx page and write the following code



<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">
<title>QueryString Example in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div><b>QueryString parameter Values in Default2.aspx Page</b></div><br />
<div><b>UserName:</b><asp:Label ID="lblUser" runat="server"/></div><br />
<div><b>Password:</b><asp:Label ID="lblpass" runat="server"/></div>
</form>
</body>

</html>

After that write the following code in code behind  in load Event For recive value from first page

protected void Page_Load(object sender, EventArgs e)

 {
       if(!IsPostBack)
         {
                  lblUser.Text = Request.QueryString["UserName"];
                  lblpass.Test = Request.QueryString["Password"];
         }

}



Thank You Friends.

No comments:

Post a Comment