RadioButtonList的结合可空VARCHAR解决方法吗? (的SelectedValue因为它不存在它无效...) [英] Binding RadioButtonList to nullable varchar workaround? (SelectedValue which is invalid because it does not exist...)

查看:93
本文介绍了RadioButtonList的结合可空VARCHAR解决方法吗? (的SelectedValue因为它不存在它无效...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表中的可为空的VARCHAR列在我的数据库。

我通过一个ObjectDataSource绑定一个gridview该表,并有一个TemplateField像这样:

I have a nullable varchar column in a table in my database.
I am binding a gridview to this table via an objectdatasource and have a TemplateField like so:

                <asp:RadioButtonList ID="rblRequirementOption" RepeatDirection="Horizontal"  runat="server" RepeatLayout="Flow"
                 SelectedValue='<%#Bind("RequirementOption")%>'>
                    <asp:ListItem Value="" Text="" style="display: none" />
                    <asp:ListItem Value="Need" Text="Need"></asp:ListItem>
                    <asp:ListItem Value="Do Not Use" Text="Do Not Use"></asp:ListItem>
                </asp:RadioButtonList>

如果有在表列的空值,我得到的错误:

'rblRequirementOption'有SelectedValue的,因为它不会在项目列表中存在哪些无效参数名称:值

If there are any null values in that column in the table, I get the error:
"'rblRequirementOption' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value"

因此​​,大家可以看到,我已尝试添加一个占位符单选按钮与价值,以适应空值,也就是通常建议的方法来处理这​​个错误,但在这种情况下,似乎 和DBNull.Value不被视为等效。

So as you can see, I have tried to add a placeholder radio button with a value of "" to accomodate the null values, which is the commonly advised method to handle this error, but in this case, it seems that "" and DBNull.Value are not considered equivalent.

当然,我没有结合之前,空值转换为一个零长度字符串操作?

Surely I don't have to convert nulls to a zero length string before binding do I?

其实,这个正确的语法来做到这一点。 (我把玩周围的默认值替代品,得到了所有混合起来。)

Actually, this is the correct syntax to do it. (I was futzing around with a default value substitute and got all mixed up.)

推荐答案

在使用768,16:

   <asp:RadioButtonList runat=server ID="rd" SelectedValue='<%# Bind("sex").GetType() == typeof(DBNull) ? null : Bind("sex") %>'
           <asp:ListItem Text="male" Value="1"></asp:ListItem>
           <asp:ListItem Text="female" Value="2"></asp:ListItem>
           </asp:RadioButtonList>

这篇关于RadioButtonList的结合可空VARCHAR解决方法吗? (的SelectedValue因为它不存在它无效...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆