Problem 2 - Web Form Using Query String

This task implements a web form that allows the user to select a customer and enter additional details. The selected data is transferred to another page using a query string.


1. CustomerForm.aspx

This is the first form where the user selects a customer, enters a city, and chooses gender.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="CustomerForm.aspx.vb" Inherits="CustomerForm" %>

<html>
<head>
    <title>Customer Form</title>
</head>
<body>
    <form id="form1" runat="server">
        <h2>Select a Customer</h2>

        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Single">
            <asp:ListItem Text="Ahmad" Value="1" />
            <asp:ListItem Text="Zain" Value="2" />
            <asp:ListItem Text="Hira" Value="3" />
            <asp:ListItem Text="Ali" Value="4" />
        </asp:ListBox>

        <br /><br />

        Enter Your City: 
        <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>

        <br /><br />

        Gender: <br />
        <asp:RadioButton ID="rdoMale" runat="server" GroupName="Gender" Text="Male" />
        <asp:RadioButton ID="rdoFemale" runat="server" GroupName="Gender" Text="Female" />

        <br /><br />

        <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
    </form>
</body>
</html>
    

2. CustomerForm.aspx.vb

This code handles the form submission and redirects to the second page with data in the query string.

Partial Class CustomerForm
    Inherits System.Web.UI.Page

    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs)
        Dim selectedCustomerName As String = ListBox1.SelectedItem.Text
        Dim selectedCustomerID As String = ListBox1.SelectedItem.Value
        Dim city As String = txtCity.Text
        Dim gender As String = ""

        If rdoMale.Checked Then
            gender = "Male"
        ElseIf rdoFemale.Checked Then
            gender = "Female"
        End If

        Response.Redirect("CustomerDetails.aspx?name=" & selectedCustomerName &
                          "&id=" & selectedCustomerID &
                          "&city=" & city &
                          "&gender=" & gender)
    End Sub
End Class
    

3. CustomerDetails.aspx

This is the second page where the values received through query string are displayed using Label controls.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="CustomerDetails.aspx.vb" Inherits="CustomerDetails" %>

<html>
<head>
    <title>Customer Details</title>
</head>
<body>
    <form id="form1" runat="server">
        <h2>Customer Details Received:</h2>

        Name: <asp:Label ID="lblName" runat="server" /><br />
        ID: <asp:Label ID="lblID" runat="server" /><br />
        City: <asp:Label ID="lblCity" runat="server" /><br />
        Gender: <asp:Label ID="lblGender" runat="server" /><br />
    </form>
</body>
</html>
    

4. CustomerDetails.aspx.vb

This code retrieves and displays the values from the query string.

Partial Class CustomerDetails
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            lblName.Text = Request.QueryString("name")
            lblID.Text = Request.QueryString("id")
            lblCity.Text = Request.QueryString("city")
            lblGender.Text = Request.QueryString("gender")
        End If
    End Sub
End Class
    

Conclusion: This lab demonstrates how to transfer selected values between two ASP.NET web forms using query strings, along with using various server controls like ListBox, TextBox, RadioButtons, and Labels.