为什么我的DropDownList无法正常工作?

时间:2015-06-25 09:27:27

标签: c# asp.net

我想显示一个DropdownList,但它还没有工作。 这是我的asp.net代码:

<asp:Panel ID="pnlChannel" runat="server">
<asp:SqlDataSource ID="sdsChannel" runat="server"></asp:SqlDataSource>
<asp:DropDownList ID="ddlChannel" runat="server"> 
    <asp:ListItem id="limDefault" runat="server"></asp:ListItem>
    </asp:DropDownList>
</asp:Panel>

然后这是我的代码隐藏:

  public Panel GetDropDownList()
{
    // Create drop down list and data source
    Panel pnlChannel = new Panel();
    DropDownList ddlChannel = new DropDownList();
    ListItem limDefault = new ListItem();
    SqlDataSource sdsChannel = new SqlDataSource();

    // Configure data source
    sdsChannel.ConnectionString = ConfigurationManager.ConnectionStrings["CR_SQL"].ConnectionString;
    sdsChannel.SelectCommand = "SELECT * FROM TABLE";
    sdsChannel.ID = "sdsChannel";

    // Configure drop down list
    ddlChannel.DataTextField = "Channel";
    ddlChannel.DataValueField = "Channel";
    ddlChannel.AppendDataBoundItems = true;
    ddlChannel.DataSourceID = "sdsChannel";



    // Configure default list item
    limDefault.Selected = true;
    limDefault.Text = "All";
    limDefault.Value = "-1";

    // Add controls to static panel in footer
    ddlChannel.Items.Add(limDefault);
    pnlChannel.Controls.Add(ddlChannel);
    pnlChannel.Controls.Add(sdsChannel);
    return pnlChannel;
}

我错过了什么吗? 所以我认为ListItem不起作用,因为有一个DropDownList但我看不到任何要删除的列表。

1 个答案:

答案 0 :(得分:0)

You have not set Text property of ListItem. Also remove code from .cs file for creating controls because this will duplicate the controls.

<asp:DropDownList ID="ddlChannel" runat="server"> 
    <asp:ListItem id="limDefault" runat="server" Text="SomeText" Value="SomeValue"></asp:ListItem>
    </asp:DropDownList>