2011-04-20 59 views
0

我的WebUserControl上有一對ddlists。在我的Page_Load代碼中,我輸入了:Dropdownlist默認值

ddlDay.Items.Insert(0, new ListItem("Day", "0")); 
    ddlMonth.Items.Insert(0, new ListItem("Month", "0")); 

    CatalogAccess ca = new CatalogAccess(); 
    ddlStudents.Items.Insert(0, new ListItem("Choose", "0")); 
    ddlStudents.DataSource = ca.GetStudents(); 
    ddlStudents.DataTextField = "FullName"; 
    ddlStudents.DataValueField = "UserID"; 
    ddlStudents.DataBind(); 

它對那些不是數據庫的ddl有效。這一個>

ddlStudents.Items.Insert(0, new ListItem("Choose", "0")); 

根本不起作用。每次我得到學生的名字。如何解決這個問題。

回答

4

您需要在ddlStudents下拉列表中設置AppendDataBoundItems="true"

+0

Shukran!它幫助! – NCFUSN 2011-04-20 17:48:08

+0

@Nathan:確保使用綠色複選框標記'最佳答案'!它是StackOverflow聲譽系統的一部分。 – 2011-04-20 17:52:36

+0

哎呀!我剛來這地方。盒子在哪裏? – NCFUSN 2011-04-20 18:02:44

0

你可以通過收集ca.GetStudents()返回的學生。

1

我相信你也應該能夠在數據綁定之後移動空白行的插入。

ddlDay.Items.Insert(0, new ListItem("Day", "0")); 
ddlMonth.Items.Insert(0, new ListItem("Month", "0")); 

CatalogAccess ca = new CatalogAccess(); 
ddlStudents.DataSource = ca.GetStudents(); 
ddlStudents.DataTextField = "FullName"; 
ddlStudents.DataValueField = "UserID"; 
ddlStudents.DataBind(); 
ddlStudents.Items.Insert(0, new ListItem("Choose", "0")); 

希望這會有所幫助。

+0

我在if(!IsPostBack){....};以避免dublicates – NCFUSN 2011-04-20 18:53:50

+0

好吧,很高興你得到它的工作。 – Dusty 2011-04-20 19:15:40

0
<asp:DropDownList id="ddlStudents" runat="server" AppendDataBoundItems="true"> 
    <asp:ListItem Selected="true" Text="Choose" Value="0"></asp:ListItem> 
</asp:DropDownList>