下拉列表未在后面的代码中给出正确的选择

时间:2012-12-31 19:44:37

标签: asp.net drop-down-menu

以下代码是我的页面的标记

<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>&nbsp;&nbsp;
                <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

1 个答案:

答案 0 :(得分:1)

这是你可以尝试的一件事。

  1. 确保在page_load方法中对if(!IsPostBack)条件中的状态下拉列表进行数据绑定。这可确保在触发选定索引更改事件时,下拉列表不会再次出现数据绑定。