从列表中显示所选项目的下拉列表

时间:2013-07-29 05:48:18

标签: c# asp.net drop-down-menu

我有一个dropdownList,它当前应该在列表中显示两个项目 - 当我选择第二个项目时它返回并显示下拉列表中的第一个项目。

这是我的代码:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDown.DataTextField = "DisplayName";
            DropDown.DataValueField = "ID";
            DropDown.DataBind();

        }
    }

<asp:DropDownList ID="DropDown" runat="server"AutoPostBack="True" DataSourceID="Sections">
            </asp:DropDownList>
            <asp:SqlDataSource ID="Sections" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnection %>" SelectCommand="SELECT e.DisplayName,  e.ID , e.GUID
                FROM .. e
                INNER JOIN .. re
                ON e.ID = re.anID
                AND re.otherID = 1">
            </asp:SqlDataSource>

2 个答案:

答案 0 :(得分:1)

从设计中删除绑定并尝试以下代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        DropDown.DataSourceID = Sections;
        DropDown.DataTextField == "DisplayName";
        DropDown.DataValueField = "ID"; 
        DropDown.DataBind();           

    }
}

谢谢, 亚太区首席技术官Matt

答案 1 :(得分:0)

只需将您的Dropdown Auto AutoPostBack设置为False。

<asp:DropDownList ID="DropDown" runat="server" DataTextField="DisplayName" DataValueField="ID" AutoPostBack="False" DataSourceID="Sections">
            </asp:DropDownList>
            <asp:SqlDataSource ID="Sections" runat="server" ConnectionString="<%$ ConnectionStrings:MyConnection %>" SelectCommand="SELECT e.DisplayName,  e.ID , e.GUID
                FROM .. e
                INNER JOIN .. re
                ON e.ID = re.anID
                AND re.otherID = 1">
            </asp:SqlDataSource>

根据您的新查询检查此网址:Populate one dropdownlist based upon action of another dropdownlist

最好的问候