以下代码是我页面的标记
<div id="addnewcontact">
<fieldset style="width:70%;">
<legend>Add New Contact Form</legend>
<asp:panel runat="server" ID="custform" cssclass="contactform visible">
"code removed for brevity"
<asp:UpdatePanel ID="updtpanlCity" runat="server">
<ContentTemplate>
<!-- State dropdown selector area -->
<asp:DropDownList ID="ddlStates" runat="server" AppendDataBoundItems="True"
AutoPostBack="True" CssClass="dropdowns" TabIndex="7" ToolTip="Select a state"
OnSelectedIndexChanged="ddlStates_SelectedIndexChanged">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvState" runat="server" ErrorMessage="[Required]"
ToolTip="Please select a state" ForeColor="#FF3300" ControlToValidate="ddlStates"
Display="Dynamic">
</asp:RequiredFieldValidator>
<!-- End of State dropdown selector area -->
<br /><p class="spacer"></p>
<asp:DropDownList ID="ddlCity" runat="server" CssClass="dropdowns"
BorderColor="Black" BorderStyle="Solid" BorderWidth="2px"
TabIndex="8" ToolTip="Select a city here"
OnSelectedIndexChanged="ddlCity_SelectedIndexChanged"
AppendDataBoundItems="True" AutoPostBack="True">
<asp:ListItem Value="" Text="Select a city"/>
</asp:DropDownList>
<br /><p class="spacer"></p>
<asp:DropDownList ID="ddlPostalCode" runat="server" TabIndex="9"
CssClass="dropdowns" ToolTip="Select your postal code here."
AppendDataBoundItems="True" AutoPostBack="True">
<asp:ListItem Value="" Text="Postal Code"/>
</asp:DropDownList>
<br />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlStates" EventName="SelectedIndexChanged"/>
<asp:AsyncPostBackTrigger ControlID="ddlCity" EventName="SelectedIndexChanged"/>
<asp:AsyncPostBackTrigger ControlID="ddlPostalCode" EventName="SelectedIndexChanged"/>
</Triggers>
</asp:UpdatePanel>
<br />
<br /><p class="spacer"></p>
<asp:Button ID="submit" runat="server" Text="Submit" CssClass="buttons" />
</asp:panel> <%--end of custform panel--%>
</fieldset>
</div>
从州下拉选择器中选择州时,代码应检索城市列表以填充城市下拉列表。这是以前工作的,但我在更改数据库结构时重建了页面。问题是我放在事件处理程序中的 msgbox 行显示索引为 0 的选择总是从该事件返回,这是为什么呢?
Protected Sub ddlStates_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlStates.SelectedIndexChanged
If IsPostBack Then
MsgBox("item: " & ddlStates.SelectedItem.ToString() & " " & "index: " & ddlStates.SelectedIndex.ToString())
Dim ctx As New enerteckwebEntities()
'retrieve the list of cities based on state selected
Dim citylist As List(Of String) = (From c In ctx.ziptaxes Where c.StateID = Convert.ToInt32(ddlStates.SelectedValue) Order By c.City Ascending Select c.City).ToList()
With ddlCity
.Items.Clear()
.DataSource = citylist.Distinct()
.DataBind()
.Items.Insert(0, "Select a city")
.SelectedIndex = 0
End With
End If
End Sub
我在标记中将 autopostback、appenddatabounditems 和 enableviewstate 设置为 true