如何让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

 

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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....