我正在使用LINQ to SQL并希望能够选择一个父代或无父代。假设我有一个包含PersonId,Name和ParentId列的People表。在ASP中,我想输入名称,选择一个父项,然后单击'添加'创建一个新的人员记录,但我也希望能够将ParentId留空。不要问为什么,这只是我想要做的简单解释。ASP.NET DropDownList绑定到数据但允许空值?
<asp:LinqDataSource ID="LinqPeople" runat="server"
ContextTypeName="MyDataContext" EntityTypeName=""
Select="new (PersonId, Name)" TableName="People"/>
<asp:TextBox ID="textName" runat="server" />
<asp:DropDownList runat="server" ID="dropParent"
DataSourceID="LinqPeople" DataTextField="Name" DataValueField="PersonId" />
<asp:Button ID="buttonAddPerson" runat="server" Text="Add" />
有没有什么办法可以在列表框中显示值为null的'None'?我想过几个选择,哪个最好,还有其他的吗?
创建的SQL存储过程 “选择PERSONID,名字从人们UNION SELECT NULL, '无'”
添加复选框 “无父” 的
把一个ListItem在标记为DropDownList
添加的数据绑定事件处理程序的项目
我发现了一个更好的办法,在标记添加列表项:
<asp:LinqDataSource ID="LinqPeople" runat="server"
ContextTypeName="MyDataContext" EntityTypeName=""
Select="new (PersonId, Name)" TableName="People"/>
<asp:TextBox ID="textName" runat="server" />
<asp:DropDownList runat="server" ID="dropParent"
DataSourceID="LinqPeople" DataTextField="Name" DataValueField="PersonId">
<asp:ListItem Value="">(none)</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="buttonAddPerson" runat="server" Text="Add" />
我找不到任何办法然而,返回的SelectedValue实际值“NULL”,但我能处理的,我猜。我认为ListItem的方式是最好的,因为它是在标记中定义的,并且不需要任何代码...
无论谁回答事件处理程序我打算接受,如果没有人发布其他任何东西...... – 2010-09-22 17:31:20