ASP   «Prev  Next»


ASP Request - Exercise Result

Use ASP to pass a query string
<html>
<head>
<title>Module 3 - Exercise 5 Solution</title>
</head>
<body>
<div align="center">
  <p><b><font size="+2">Module 3 - Exercise 5 Solution
  </font></b></p>
  <% Response.Write "Welcome "
  & Request.QueryString("txtName")  & "!" %>
</div>
</results>
</html>

<form name="frmWelcome" method="GET" action="">

Use ASP.NET to pass input Data in a Query String

In ASP.NET, you can pass input data using a query string, which appends key-value pairs directly to the URL. Here's how you can achieve this step by step:
Step 1: Create a form or input control in your ASP.NET page:
Example HTML/ASP.NET form (WebForms):
<form id="form1" runat="server" method="get" action="TargetPage.aspx">
    Enter Name: <input type="text" name="username" />
    <input type="submit" value="Submit" />
</form>

Step 2: Sending data via query string from Code-Behind (C#):
You can construct and redirect to a URL manually in the code-behind file:
protected void SubmitButton_Click(object sender, EventArgs e)
{
    string username = UsernameTextBox.Text;
    Response.Redirect("TargetPage.aspx?username=" + Server.UrlEncode(username));
}

Step 3: Receiving the query string data:
On the target ASP.NET page, you can retrieve the query string parameter:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        string username = Request.QueryString["username"];

        if (!string.IsNullOrEmpty(username))
        {
            Label1.Text = "Hello, " + username;
        }
    }
}

Important Points:
  • Encoding:
    • Always encode your query string values using Server.UrlEncode() to safely pass special characters.
  • Security:
    • Query strings are visible to users. Never pass sensitive information through query strings.
    • Validate input thoroughly on receiving pages.

Example Query String URL after submission:
http://yourwebsite.com/TargetPage.aspx?username=JohnDoe
This way, you effectively use query strings in ASP.NET for passing data between pages.

Remote 1