将ajax控件转换为下拉列表

时间:2009-12-03 01:01:12

标签: asp.net asp.net-ajax

情况 - > 使用了两个ajax控件....

第一个

充当提示符,即人物类型在文本框中表示字母l,并且以l开头的选项被显示以供用户单击并选择。

第二个ajax控件

也可以作为提示符,也就是说,当您在第二个文本框中键入字母l时,它会显示选项,但仅限于在第一个文本框中选择的选项中可用的选项。

我想将第二个ajax控件转换为下拉列表,也就是说,所有选项应该出现在第一个文本框中选择的选项中,但是作为下拉列表而不是提示符。如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

我会将2个DropDownLists中的每一个放在各自的UpdatePanels中,并将初始TextBox放在外面:

    <asp:TextBox ID="tbLetterChoice" MaxLength="1" runat="server" AutoPostBack="true" OnTextChanged="ShowDropDown1Options" />

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="tbLetterChoice" EventName="TextChanged" />
            </Triggers>
            <ContentTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ShowDropDown2Options" />
            </ContentTemplate>  
        </asp:UpdatePanel>

        <asp:UpdatePanel ID="UpdatePanel2" runat="server">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="DropDownList1" />
            </Triggers>
            <ContentTemplate>
                <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DoSomething" />
            </ContentTemplate>
        </asp:UpdatePanel>

在代码隐藏中需要两个小方法来填充DropDownLists:

protected void ShowDropDown1Options(object sender, EventArgs e){
    DropDownList1.Items.Clear();
    // populate DropDownList1 based on tbLetterChoice.Text
}

protected void ShowDropDown2Options(object sender, EventArgs e){
    DropDownList2.Items.Clear();
    // populate DropDownList2 based on DropDownList1.SelectedValue
}

protected void DoSomething(object sender, EventArgs e){
    // take action based on final choice from DropDownList2
}

因此,用户在'tbLetterChoiceand'中键入一个字母,然后输入。这会更新'DropDownList1'。用户从'DropDownList1'中选择项目,并填充'DropDownList2'。用户从'DropDownList2'中选择项目,程序执行'DoSomething()'。

相关问题