如何让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
注意: 首选静态声明,因为动态绑定在显示空值的时候可能会出现问题。