How to Use QueryString in Asp.Net

How to Use QueryString in Asp.Net

QueryString in Asp.Net:-

For passing variables content from PAGE A  > PAGE B in ASP.NET gives us several choices. One of the choice from all of that is using QueryString.

  1. A query string is additional string that is appended at the end of page link.
  2. Query strings fulfill our limited needs to maintain state information.
  3. Never rely on query strings to convey important or sensitive data, because data can be tampered by a user at any point.

To demonstrate querystring here we take two aspx pages. Let them call Page 1 and Page2.

Page 1 Code:-

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void querystring_Click(object sender, EventArgs e)
{
Response.Redirect("Page 2.aspx?pid="+txtboxid.Text);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server"> <title>Page 1</title> </head>
<body> <form id="form1" runat="server"> <div>
<asp:TextBox ID="txtboxid" runat="server"></asp:TextBox>
<asp:Button ID="querystring" runat="server" Text="Transfer Data" onclick="querystring_Click" />
</div> </form>
</body>
</html>


 

Page 2 Code:-

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{ string pid_1 = Request.QueryString["pid"].ToString();
Response.Write(pid_1);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Page 2</title>
</head>
<body> <form id="form1" runat="server"> <div> </div> </form> </body>
</html>


 

Now enjoy……