-2

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

这是我的代码:

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>
4

2 回答 2

1

Remove binding from design and try below code:

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

    }
}

Thanks, Hitesh

于 2013-07-29T06:08:30.407 回答
0

只需将您的 Dropdown 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>

根据您的新查询检查此 URL:Populate one dropdownlist based on action of another dropdownlist

此致

于 2013-07-29T05:51:40.417 回答