所以我試圖做一個DropDownList。當我在aspx文件中執行它時,它正在工作,但它在代碼隱藏內部不起作用。DropDownList不能與代碼隱藏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;
}
爲什麼沒有它的工作:
<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#代碼隱藏嘗試過嗎? +我不會用這兩個,我想使用基於您的評論,這些意見
定義「不工作」。它在做什麼?你期望它做什麼?當你在調試器中通過時,哪裏出錯? – David
爲什麼你在你的代碼中創建一個新的下拉列表?你可以使用現有的通過使用ddlChannel,而不是將其設置爲一個新的DropdownList ... –
@David如果如果它在代碼隱藏中有一個DropDownList但列表中沒有任何內容。所以我認爲itemlist是錯誤的,但我找不到任何錯誤。 –