如何让DropDownList 绑定null(空值)

本文介绍如何在ASP.NET的DropDownList控件中通过静态声明及动态方式添加一个空值选项,确保用户可以选择无或空的状态。文章还提供了一个在GridView中的DropDownList控件添加空值项的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

静态声明:

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ObjectDataSource1"

 

                    DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("ParentID") %>'

 

                    Style="position: relative" AppendDataBoundItems="True">

 

                   <asp:ListItem Value="">(无 或 空 或 all)</asp:ListItem><!--需要增加的内容-->

 

 </asp:DropDownList>

 

动态添加:

<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ObjectDataSource1"

 

                    DataTextField="Name" DataValueField="ID" SelectedValue='<%# Bind("ParentID") %>'

 

                    Style="position: relative" AppendDataBoundItems="True">

 

 

 </asp:DropDownList>

以下为包含在GridView中的DropDownList添加一个空值项:

Protected Sub GV_Match_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GV_Match.RowDataBound
        If (e.Row.RowType = DataControlRowType.Header Or e.Row.RowType = DataControlRowType.DataRow) Then
                 Dim ddl As DropDownList = New DropDownList()
                ddl = e.Row.FindControl("DropDownList1")
                If Not ddl Is Nothing Then
                    ddl.Items.Insert(0,new ListItem("NULL",String.Empty))

                End If
         End If
 End Sub

 

注意: 首选静态声明,因为动态绑定在显示空值的时候可能会出现问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值