我希望我的线程的标题是有意义的。
我们正在构建一个汽车经销商网络应用程序,允许潜在的汽车购买者确定哪种类型的汽车适合他们的预算和需求。
因此,我们有汽车制造商、汽车型号、汽车状况、里程、价格范围等的下拉列表。
下面的代码显示了汽车品牌的下拉列表。
当用户选择他/她喜欢的汽车品牌时,与该品牌相关的模型会自动加载到第二个下拉列表中。
<form id="form1" runat="server">
<div>
<span style ="font-family:Arial">Select Make : </span>
<asp:DropDownList ID="ddlMake" runat="server" AutoPostBack = "true" OnSelectedIndexChanged="ddlMake_SelectedIndexChanged">
<asp:ListItem Text = "------" Value = ""></asp:ListItem>
</asp:DropDownList>
<br /><br />
<span style ="font-family:Arial">Select Model : </span>
<asp:DropDownList ID="ddlModel" runat="server" Enabled = "false" AutoPostBack = "true" OnSelectedIndexChanged="ddlModel_SelectedIndexChanged">
<asp:ListItem Text = "------" Value = ""></asp:ListItem>
</asp:DropDownList>
<br /><br />
<asp:button id="btnSave" runat="server" Text="Submit New Request"></asp:button><br /><br />
<asp:Label ID="lblResults" runat="server" Text="" Font-Names = "Arial"></asp:Label>
</div>
</form>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
ddlMake.AppendDataBoundItems = True
Dim strConnString As [String] = ConfigurationManager _
.ConnectionStrings("amsconstr").ConnectionString
Dim strQuery As [String] = "select vehicleId, Make from MakeModel"
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = strQuery
cmd.Connection = con
Try
con.Open()
ddlMake.DataSource = cmd.ExecuteReader()
ddlMake.DataTextField = "Make"
ddlMake.DataValueField = "vehicleId"
ddlMake.DataBind()
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
End If
End Sub
Protected Sub ddlMake_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
ddlModel.Items.Clear()
ddlModel.Items.Add(New ListItem("--Select Model--", ""))
ddlModel.AppendDataBoundItems = True
Dim strConnString As [String] = ConfigurationManager _
.ConnectionStrings("amsconstr").ConnectionString
Dim strQuery As [String] = "select vehicleId Employee from MakeModel " _
& "where vehicleId=@vehicleId"
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.Parameters.AddWithValue("@vehicleId", _
ddlMake.SelectedItem.Value)
cmd.CommandType = CommandType.Text
cmd.CommandText = strQuery
cmd.Connection = con
Try
con.Open()
ddlModel.DataSource = cmd.ExecuteReader()
ddlModel.DataTextField = "Model"
ddlModel.DataValueField = "VehicleId"
ddlModel.DataBind()
If ddlModel.Items.Count > 1 Then
ddlModel.Enabled = True
Else
ddlModel.Enabled = False
End If
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
End Sub
Protected Sub ddlModel_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim strConnString As [String] = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Dim strQuery As [String] = "select VehicleId, Model from MakeModel where VehicleId=@vehicleId"
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
cmd.Parameters.AddWithValue("@vehicleId", ddlModel.SelectedItem.Value)
cmd.CommandType = CommandType.Text
cmd.CommandText = strQuery
cmd.Connection = con
Try
con.Open()
Catch ex As Exception
Throw ex
Finally
con.Close()
con.Dispose()
End Try
End Sub
这工作正常。
我们想为每个品牌的汽车附加可用记录的数量。
例如,在 Make of a car 下拉列表中,我们有如下内容:
马自达奔驰丰田等
我们希望用户知道特定品牌如何可用,例如:
Mazda (10)
Mercedez Benz (2)
Toyota (7)
如何为特定品牌的汽车添加可用汽车数量?