2008-12-31 28 views
1

我的應用程序中有一個下拉列表控件,當我從數據庫中添加項目時它默認顯示第一個項目到下拉列表中,但我想顯示其他文本像這樣「從列表中選擇項目」有什麼辦法可以做到這一點。如何在ASP.Net中將自定義文本設置爲DropdownList

也可以請你幫我從JavaScript

回答

7

設置相同的值對事物的ASP.NET的一面,你可以創建AppendDataBoundItems將DropDownList =「true」,並綁定到任何物品會後默認:

function addFirstItem(list, text, value) 
{ 
    var newOption = document.createElement("option"); 
    newOption.text = text; 
    newOption.value = value; 
    list.options.add(newOption); 
} 

addFirstItem(document.getElementById("yourListId"), "Select something", "-1"); 

或者爲J:

<asp:DropDownList AppendDataBoundItems="true" ID="yourListId" runat="server"> 
    <asp:ListItem Text="Select something" Value="-1" /> 
</asp:DropDownList> 

至於在Javascript完全做同樣的事情,你可以用這樣的功能做查詢(有可能是一些乾淨多了,尤其是對於創建一個新的選項標籤,但這個工程):

$("#yourListId option:first").before("<option value='-1'>Select something</option>"); 
+1

addFirstItem(document.getElementById(「yourListId」),「Select something」,「-1」); 給出的錯誤說明該對象預期爲 – RBS 2008-12-31 18:03:45

0

帕特里奇答案是正確的,但如果您使用的是asp的方法,仍然遇到了問題,加項目標記到listitem。

<asp:DropDownList AppendDataBoundItems="true" ID="yourListId" runat="server"> 
    <items> 
    <asp:ListItem Text="Select something" Value="-1">--Select Something--</asp:ListItem> 
    </items> 
</asp:DropDownList> 
相關問題