【问题标题】:Why can I not get this code to work in My Gridview为什么我不能让此代码在 My Gridview 中工作
【发布时间】:2017-09-14 14:33:04
【问题描述】:

我正在使用 Telerix RadGrid,似乎无法获取代码来提供复选框的值。这是我的 C# 代码

protected void gvMembers_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        var editableItem = ((GridEditableItem)e.Item);
        var memberId = (int)editableItem.GetDataKeyValue("UserID");
        int Role = 1;
        CheckBox active = (CheckBox)editableItem["valid"].Controls[0];
        Boolean boolactive = Convert.ToBoolean(active.Checked = !String.IsNullOrEmpty(e.Item.Cells[0].Text));
        string strFirstName = (editableItem["firstname"].Controls[0] as TextBox).Text;
        string strLastName = (editableItem["lastname"].Controls[0] as TextBox).Text;
        string strUserName = (editableItem["username"].Controls[0] as TextBox).Text;
        string strEmail = (editableItem["firstname"].Controls[0] as TextBox).Text;

代码编译并运行,但复选框值始终为 false。我错过了什么?

我进行了更多故障排除并发现了以下代码:

CheckBox active = (CheckBox)editableItem["valid"].Controls[0];

返回这个值 {Text = "" Checked = true}

然后我将其添加为我的下一行代码:

 string str = active.Text;

并检查了它返回一个空字符串的字符串的值。

关于如何从复选框中获取布尔值以便将其传递给数据库的任何想法?

【问题讨论】:

  • 不明白你的情况active.Checked = !String.IsNullOrEmpty(e.Item.Cells[0].Text
  • 也许你的意思是==而不是=
  • 你没有将赋值返回存储到 bool 吗?我认为您的意思是:active.Checked = !String.IsNullOrEmpty(e.Item.Cells[0].Text); 然后Boolean boolactive = Convert.ToBoolean(active.Checked); 或其他方式做同样的bool? boolactive = active.Checked = !String.IsNullOrEmpty(e.Item.Cells[0].Text);
  • 感谢您的 cmets 我做了更多检查,看起来 !String.IsNullOrEmpty(e.Item.Cells[0].Text) 的返回值是一个空字符串。看起来我没有得到单元格的值。

标签: c# gridview radgrid


【解决方案1】:

我能够通过使用以下获取布尔值来解决问题:

            CheckBox active = (CheckBox)editableItem["valid"].Controls[0];
        Boolean bactive = active.Checked;

感谢您的 cmets。

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 2018-03-27
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多