如何在ASP.Net中将自定义文本设置为DropdownList

如何在ASP.Net中将自定义文本设置为DropdownList,asp.net,drop-down-menu,Asp.net,Drop Down Menu,我的一个应用程序中有下拉列表控件,当我从数据库中添加项目时,默认情况下,它会将第一个项目显示到下拉列表中,但我想在其中显示一些其他文本,如从列表中选择项目。是否有任何方法可以做到这一点 您还可以帮助我从javascript设置相同的值吗?在ASP.NET方面,您可以使用AppendDataBoundItems=true创建DropDownList,并且您绑定到它的任何项目都将位于默认值之后: <asp:DropDownList AppendDataBoundItems="true" ID=

我的一个应用程序中有下拉列表控件,当我从数据库中添加项目时,默认情况下,它会将第一个项目显示到下拉列表中,但我想在其中显示一些其他文本,如从列表中选择项目。是否有任何方法可以做到这一点


您还可以帮助我从javascript设置相同的值吗?在ASP.NET方面,您可以使用AppendDataBoundItems=true创建DropDownList,并且您绑定到它的任何项目都将位于默认值之后:

<asp:DropDownList AppendDataBoundItems="true" ID="yourListId" runat="server">
    <asp:ListItem Text="Select something" Value="-1" />
</asp:DropDownList>
或者使用jQuery,可能会有更干净的东西,特别是在创建新的选项标记时,但这是可行的:

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

在ASP.NET方面,您可以使用AppendDataBoundItems=true创建DropDownList,并且您绑定到它的任何项目都将位于默认值之后:

<asp:DropDownList AppendDataBoundItems="true" ID="yourListId" runat="server">
    <asp:ListItem Text="Select something" Value="-1" />
</asp:DropDownList>
或者使用jQuery,可能会有更干净的东西,特别是在创建新的选项标记时,但这是可行的:

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

Patridge answer是正确的,但是如果您使用asp方法但仍然遇到问题,请将items标记添加到listitem

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

Patridge answer是正确的,但是如果您使用asp方法但仍然遇到问题,请将items标记添加到listitem

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

addFirstItemdocument.getElementByIdyourListId,选择一些东西,-1;如果这是生成错误的行,那么实际上需要一个ID=yourListId的列表,或者更改getElementById中使用的ID。我只是把yourListId作为占位符放进去。如果addFirstItem中有一行出错,请告诉我。addFirstItemdocument.getElementByIdyourListId,选择-1;如果这是生成错误的行,那么实际上需要一个ID=yourListId的列表,或者更改getElementById中使用的ID。我只是把yourListId作为占位符放进去。如果addFirstItem中有一行出错,请告诉我。