如何让DropDownList 绑定null(空值)

静态声明:

<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

 

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

posted @   emanlee  阅读(2857)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
点击右上角即可分享
微信分享提示