页面加载时不显示任何内容

时间:2012-08-09 16:16:28

标签: c# asp.net

Dropdownlist从数据库中读取其值并显示它们。当页面加载dropdownlist什么都没有显示(即selectedindex = -1)和用户选择值时,我想要。任何想法?

2 个答案:

答案 0 :(得分:3)

添加空的未绑定项

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

答案 1 :(得分:0)

您可以从后端添加空数据列表项,也可以直接从下拉列表中添加:

从数据库侧:

SELECT
  1 as SortOrder,
  DataValueField,
  DataTextField
FROM
  YourTable
UNION ALL
SELECT
  0 As SortOrder,       --this ensures the empty item is at the top of the list
  0 As DataValueField,
  '' As DataTextField --or 'Select One'
Order By
  SortOrder,
  Value

或直接作为列表项

<asp:ListItem Selected="True" Text="Select One" Value="0" AppendDataBoundItems="true" />