locked
Could not check the state of checkbox present in grid view RRS feed

  • Question

  • User-1415774685 posted

    var gridData = document.getElementById("MainContent_gridRequests");

     if (gridData.rows[row].cells[7].firstChild.checked == true)

    The above code is a part of my javascript code, where i try to read the state of checkbox whether it is checked or not. it throws the error undefined. this checkbox is a template field in my grid view. please let me know a solution.

    Thank you!

    Sunday, June 1, 2014 1:32 AM

Answers

  • User364480375 posted

    sample code:

    var grid = document.getElementById("gridId");  //Retrieve the grid    
        var inputs = grid.getElementsByTagName("input"); //Retrieve all the input elements from the grid
        var isValid = false;
        for (var i=0; i < inputs.length; i += 1) { //Iterate over every input element retrieved
            if (inputs[i].type === "checkbox") { //If the current element's type is checkbox, then it is wat we need
                if(inputs[i].checked === true) { //If the current checkbox is true, then atleast one checkbox is ticked, so break the loop
                    isValid = true;
        			break;
                }
            }
        }
        if(!isValid) {
        	alert('Check at least one checkbox');
        }
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 2, 2014 7:23 AM
  • User-417640953 posted

    var gridData = document.getElementById("MainContent_gridRequests");

     if (gridData.rows[row].cells[7].firstChild.checked == true)

    Hi Jeevanathan,

    Thanks for your post.

    Based on your description, I see you want to get the checkbox's check status which in gridview.

    your code looks fine in my side, please check whether your gridview layout fits your javascript code.

    Below is a working demo made for you, please check it.

    <head runat="server">
        <title></title>
        <script>
            function GetCheckBoxStatus()
            {
                var gridData = document.getElementById("GridView1");
    
                //alert(gridData.rows[3].cells[0].firstChild.checked==true);
                
                if (gridData.rows[3].cells[0].firstChild.checked == true)
                {
                    alert("checked!");
                }
    
    
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="Button1" type="button" value="GetCheckBoxStatus" onclick="GetCheckBoxStatus()" />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="ID">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate><asp:CheckBox runat="server" Checked="false"></asp:CheckBox></ItemTemplate> 
                    </asp:TemplateField>
                     <asp:TemplateField>
                        <ItemTemplate><%#Eval("Name") %></ItemTemplate> 
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
           
        </div>
        </form>
    </body>

    code behind:

      protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) {
                      //test data
                    DataTable dt = new DataTable();
                    dt.Columns.Add("ID");
                    dt.Columns.Add("Name");
                    dt.Rows.Add(1,"Jhon1");
                    dt.Rows.Add(2, "Jhon2");
                    dt.Rows.Add(3, "Jhon3");
                    dt.Rows.Add(4, "Jhon4");
                    dt.Rows.Add(5, "Jhon5");
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
    
            }

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 3, 2014 3:52 AM

All replies

  • User364480375 posted

    sample code:

    var grid = document.getElementById("gridId");  //Retrieve the grid    
        var inputs = grid.getElementsByTagName("input"); //Retrieve all the input elements from the grid
        var isValid = false;
        for (var i=0; i < inputs.length; i += 1) { //Iterate over every input element retrieved
            if (inputs[i].type === "checkbox") { //If the current element's type is checkbox, then it is wat we need
                if(inputs[i].checked === true) { //If the current checkbox is true, then atleast one checkbox is ticked, so break the loop
                    isValid = true;
        			break;
                }
            }
        }
        if(!isValid) {
        	alert('Check at least one checkbox');
        }
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 2, 2014 7:23 AM
  • User-417640953 posted

    var gridData = document.getElementById("MainContent_gridRequests");

     if (gridData.rows[row].cells[7].firstChild.checked == true)

    Hi Jeevanathan,

    Thanks for your post.

    Based on your description, I see you want to get the checkbox's check status which in gridview.

    your code looks fine in my side, please check whether your gridview layout fits your javascript code.

    Below is a working demo made for you, please check it.

    <head runat="server">
        <title></title>
        <script>
            function GetCheckBoxStatus()
            {
                var gridData = document.getElementById("GridView1");
    
                //alert(gridData.rows[3].cells[0].firstChild.checked==true);
                
                if (gridData.rows[3].cells[0].firstChild.checked == true)
                {
                    alert("checked!");
                }
    
    
            }
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <input id="Button1" type="button" value="GetCheckBoxStatus" onclick="GetCheckBoxStatus()" />
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="ID">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate><asp:CheckBox runat="server" Checked="false"></asp:CheckBox></ItemTemplate> 
                    </asp:TemplateField>
                     <asp:TemplateField>
                        <ItemTemplate><%#Eval("Name") %></ItemTemplate> 
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
           
        </div>
        </form>
    </body>

    code behind:

      protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) {
                      //test data
                    DataTable dt = new DataTable();
                    dt.Columns.Add("ID");
                    dt.Columns.Add("Name");
                    dt.Rows.Add(1,"Jhon1");
                    dt.Rows.Add(2, "Jhon2");
                    dt.Rows.Add(3, "Jhon3");
                    dt.Rows.Add(4, "Jhon4");
                    dt.Rows.Add(5, "Jhon5");
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
    
            }

    Hope that helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 3, 2014 3:52 AM