簡體   English   中英

填充下拉列表

[英]populate dropdownlist

嗨,我需要填充一個下拉列表。 我設計了一個數據源,並將其分配給dropdownlist。 下拉列表正確填充。 但是問題是我需要在dropdownlist的開頭添加一個默認值,例如“ default”(並且此值的默認值不在數據庫中。

我是這樣做的:

 <asp:DropDownList ID="classInstructor" runat="server" DataSourceID="SqlDataSource3" 
                DataTextField="InstrName" DataValueField="InstrName">

        <asp:ListItem Value="Default" Text="Default" Selected="True"></asp:ListItem>


        </asp:DropDownList>

但是默認值不會在下拉菜單中顯示。 可能是我做錯了。 你能告訴我最好的處理方法嗎?

在下拉列表AppendDataBoundItems屬性設置為true ,數據源中的項目將出現在您在標記中添加的所有ListItem之后。

<asp:DropDownList ID="classInstructor" runat="server" DataSourceID="SqlDataSource3" 
    DataTextField="InstrName" DataValueField="InstrName" AppendDataBoundItems="true">
    <asp:ListItem Value="Default" Text="Default" Selected="True"/>
</asp:DropDownList>

如果DropDownList在運行時綁定到數據源,則不能使用此方法,而在設計時標記中包含的Default項目將在綁定中被洗掉。

您需要做的是在調用DataBind()方法之后Insert一個。

看到這里的例子和對此的更多評論: Asp.net-在下拉列表頂部添加空白項

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM