locked
Why can I not get the value of a checkbox in my Gridview RRS feed

  • Question

  • I am using a gridview that has a check box.  I am trying to update the record in the database using this code.

    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;

    The code runs but the value of the checkbox is always false.  What am I doing wrong here?

    I have done some more troubleshooting and have added another line of code.  Code is now this:

    var editableItem = ((GridEditableItem)e.Item);
                var memberId = (int)editableItem.GetDataKeyValue("UserID");
                int Role = 1;
                CheckBox active = (CheckBox)editableItem["valid"].Controls[0];
                string str = active.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;
    I added a string to get the text value of the checkbox.  When I inspect the value of string str it is an empty string.  When I inspect the value of the checkbox I am seeing this value: {Text = "" Checked = true}  How can I get that checked = true value as boolean


    • Edited by Perry Pierce Thursday, September 14, 2017 3:15 PM added more code and info
    • Moved by CoolDadTx Thursday, September 14, 2017 11:54 PM ASP.NET related
    Thursday, September 14, 2017 2:27 PM

All replies

  • I was able to get the answer to my question.  To get the value I had to change my code from

    string str = active.Text

    To This: 
                    Boolean bactive = active.Checked;
    Now getting the correct boolean data.
    Thursday, September 14, 2017 6:16 PM
  • Please post questions related to ASP.NET in the ASP.NET forums.
    Thursday, September 14, 2017 11:54 PM