In this article we are leanin about ASP.NET Query String.
Query String is passed in the URL. They store the value in the
form of Key-Value pair. We will have to maintain the Query Strings
when we pass it from one page to second page and second page to the third page.
when redirecting a request from one page to another page, we can pass the Query
String.
C# Code
http://localhost:1494/FrmQueryString.aspx?
=abc&
=xyz
In query strings, values are stored at the end of the URL. These values are
visible to the user through his or her browser’s address bar. Query strings are
not secure. You should not send secret information through the query string.
Sending Query String
Response.Redirect("FrmQueryString.aspx?UserName=" + txtUserName.Text +
",address=" + txtAddress.Text);
Retrieving Query String
string s = Request.QueryString["UserName"].ToString();
In below Code show how to read value from URL query string.
Code: FrmQueryString.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FrmQueryString.aspx.cs"
Inherits="LearnAsp.Net.FrmQueryString" %>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Code: FrmQueryString.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace LearnAsp
{
public partial class FrmQueryString : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString.Count>0)
{
string UserName = Request.QueryString["UserName"];
string Address = Request.QueryString["Address"];
Response.Write("User Name : " + UserName);
Response.Write("
Address : " + Address);
}
}
}
}
Output:
No comments:
Post a Comment
Please do not enter any spam link in the comment box.