所以我尝试制作一个DropDownList。当我在aspx文件中执行它时它正在工作,但它在代码隐藏中不起作用。
<asp:Panel ID="pnlChannel" runat="server">
<asp:SqlDataSource ID="sdsChannel" runat="server" ConnectionString="<%$ ConnectionStrings:Monitor_SQL %>" SelectCommand="SELECT * FROM TABLE"></asp:SqlDataSource>
<asp:DropDownList ID="ddlChannel" runat="server" DataSourceID="sdsChannel" AutoPostBack="True" DataTextField="Kanal">
</asp:DropDownList>
</asp:Panel>
然后我用C#中的代码隐藏尝试了它:
public Panel GetDropDownList()
{
// Create drop down list and data source
Panel pnlChannel = new Panel();
pnlChannel.ID = "pnlChannel";
DropDownList ddlChannel = new DropDownList();
ddlChannel.ID = "ddlChannel";
ListItem limDefault = new ListItem();
SqlDataSource sdsChannel = new SqlDataSource();
sdsChannel.ID = "sdsChannel";
// Configure data source
sdsChannel.ConnectionString = ConfigurationManager.ConnectionStrings["Monitor_SQL"].ConnectionString;
sdsChannel.SelectCommand = "SELECT * From TABlE";
// Configure drop down list
ddlChannel.DataTextField = "Kanal";
ddlChannel.DataValueField = "Kanal";
ddlChannel.AppendDataBoundItems = true;
ddlChannel.DataSourceID = "sdsChannel";
ddlChannel.AutoPostBack = true;
// Configure default list item
limDefault.Selected = true;
limDefault.Text = "Alle";
limDefault.Value = "-1";
//Add controls to static panel in footer
ddlChannel.Items.Add(limDefault);
pnlChannel.Controls.Add(ddlChannel);
pnlChannel.Controls.Add(sdsChannel);
return pnlChannel;
}
为什么不工作? +我不会同时使用这两种,我想使用其中一种观点
答案 0 :(得分:0)
根据您的评论:
如果在代码隐藏中执行此操作,则会有一个DropDownList,但列表中没有任何内容
您从未将控件绑定到数据:
ddlChannel.DataBind();