我在EditTemplate中使用下拉列表,如下所示:
<EditItemTemplate>
<asp:DropDownList ID="ddlBusinessType" runat="server" DataSourceID="BusinessTypeSource" DataTextField="Value" DataValueField="Value" AppendDataBoundItems="true" Text='<%# Bind("BusinessType") %>'>
<asp:ListItem>Please Select</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>DataSource具有以下值:
Personal
Professional我遇到的问题是,我绑定的字段有一个空白值。由于空白值不在DataSource中,因此我得到以下错误消息:
'ddlBusinessType‘有一个无效的SelectedValue,因为它不存在于项目列表中。参数名称:值
不知道怎么解决这个问题。当我执行绑定时,如果该值不在数据源中,则我喜欢将其默认为“请选择”
发布于 2013-11-26 23:55:19
这取决于具体情况,但我经常使用的一个选项如下:
1)在查找表中,我将有一个名为“”的条目,其主键值为0(或其他什么)。
2)在事务表中,将该字段的所有空值更新为0(或“未选中”选项的值)。另外,将该字段的默认值设置为0。
这是好的,有几个原因-一个,没有特殊的标记-需要(所以你知道它将永远是一致的)-和第二个,无论你使用这些数据,你可以访问‘未选择’的输出。
发布于 2013-11-26 23:11:41
它应该是:
<EditItemTemplate>
<asp:DropDownList ID="ddlBusinessType" runat="server"
DataSourceID="BusinessTypeSource"
DataTextField="BusinessType" DataValueField="Value" AppendDataBoundItems="true" >
<asp:ListItem Value="-1">Please Select</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>1)不需要设置文本,因为您已经限制了文本字段。
2)您需要为在标记中添加的默认项设置一个值。
发布于 2013-11-27 19:09:41
您正在尝试在绑定处理之前选择一个项。捕获绑定事件并在此位置设置de选定值。
https://stackoverflow.com/questions/20227570
复制相似问题