是否可以从 DetailsView 中的 CheckboxField 中提取选中状态?

发布于 2024-12-19 09:06:34 字数 405 浏览 2 评论 0原文

最初我有一个 TemplateField ,其中有一个复选框,但我无法使用

Checked='<%# (bool)Bind("FieldName")  %>

Checked='<%# (bool)Eval("FieldName")  %>

它不断抛出无效的强制转换异常来设置复选框值。数据库中的字段是设置为 1 或 0 的位字段。

我尝试切换到复选框字段,但因为我的更新代码位于代码隐藏中,而不是使用数据源中的 updatecommand 参数,所以我似乎无法从中检索值要传递给我的业务逻辑的 CheckBoxField。

任何人都可以告诉我如何从详细信息视图中的 CheckBox 字段中检索值吗?

Originally I had a TemplateField with a CheckBox in it but I couldn't set the checkbox Value using

Checked='<%# (bool)Bind("FieldName")  %>

or

Checked='<%# (bool)Eval("FieldName")  %>

It kept throwing an invalid cast exception. The field in the database is a bit field set to 1 or 0.

I tried switching to a checkbox field but because my update code is in the codebehind instead of using the updatecommand parameters in a datasource I can't seem to retrieve the value from the CheckBoxField to pass to my business logic.

Can any one point me on how to retrieve the value from the CheckBoxfield in a detailsview?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

洋洋洒洒 2024-12-26 09:06:34

你不能从 int 转换为 bool。尝试 Convert.ToBoolean(Eval("FieldName")) 不确定是否可以像这样使用 Eval ,但这就是一般想法。

You can't cast from int to bool. Try Convert.ToBoolean(Eval("FieldName")) Not sure if you can use Eval like that though, but that's the general idea.

计㈡愣 2024-12-26 09:06:34

我认为您需要对数据库中的值使用 ToString() ,因为 HTML 正在查找 True 或 False,而不是 1 或 0。

I think that you need to use ToString() on the value from the database, since the HTML is looking for True or False, not 1 or 0.

面如桃花 2024-12-26 09:06:34

您可以使用

CheckBox ckBox = ucDetailView.FindControl("CheckBoxID") as CheckBox
if(ckBox != null){
   ckBox.Checked = (bool)datasource["FieldName"].ToString()
   //.. or some better casting code with more null checks
}

然后在后面的代码中设置它吗?解决方案似乎有效此处

Could you use

CheckBox ckBox = ucDetailView.FindControl("CheckBoxID") as CheckBox
if(ckBox != null){
   ckBox.Checked = (bool)datasource["FieldName"].ToString()
   //.. or some better casting code with more null checks
}

Then set it from there in code behind. Solution seemed to work here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文