asp.net 下拉列表 - 在 db 值之前添加空行 [英] asp.net dropdownlist - add blank line before db values

查看:23
本文介绍了asp.net 下拉列表 - 在 db 值之前添加空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的页面上,我有一个 DropDownList,我用来自 SqlDataSource 的数据库值填充它(见下面的代码).

On my page I have a DropDownList which I populate with database values from an SqlDataSource (see code below).

如何在值前添加我自己的文本或空行?

How can I add my own text or a blank line before the values?

<asp:DropDownList ID="drpClient" runat="server" Width="200px" 
    AutoPostBack="True" DataSourceID="dsClients" DataTextField="name" 
        DataValueField="client_id">
</asp:DropDownList>
<asp:SqlDataSource ID="dsClients" runat="server" 
    ConnectionString="my_connection_string" 
    ProviderName="System.Data.SqlClient" 
    SelectCommand="SELECT [client_id], [name] FROM [clients]">
</asp:SqlDataSource>

谢谢.

附言你推荐使用 SqlDataSource 还是用另一种方式填充更好?

P.S. Do you recommend using a SqlDataSource or is it better to populate another way?

推荐答案

您只需添加一个 ListItem 在 DropDownList 标记中.之后将附加来自 DataSource 的所有值.

You can simply add a ListItem inside the DropDownList Markup. All the values from the DataSource will be appended after that.

<asp:DropDownList ID="drpClient" runat="server" Width="200px" 
          AutoPostBack="True" DataSourceID="dsClients" DataTextField="name" 
          DataValueField="client_id" AppendDataBoundItems="true">
   <asp:ListItem>-- pick one --</asp:ListItem>
</asp:DropDownList>

这篇关于asp.net 下拉列表 - 在 db 值之前添加空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆