javascript : verify if checkboxes in a particular column in datagrid is checked

I  have a webpage with datagrid. Controls to the grid are added dynamically. one of the control is a checkbox.Let's say it is Col C on the datagrid. There is one more checkbox column E. On submit, I want to verify that alleast one check box in this particular column C is checked. How to write a client side Javascript for this.
Kiran_MAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

itzdarcCommented:
To be fair, this is a little more work than i think 250 points is worth, but I provided it anyway.  :)
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
    
    // Just used to make a temporary record set.
    protected DataTable GetDataTable()
    {
        DataTable dt = new DataTable("Table");
 
        dt.Columns.Add("Test", typeof(string));
 
        DataRow r = dt.NewRow();
        r["Test"] = "1";
        dt.Rows.Add(r);
 
        r = null;
        r = dt.NewRow();
 
        r["Test"] = "2";
        dt.Rows.Add(r);
 
        r = null;
        r = dt.NewRow();
 
        r["Test"] = "3";
        dt.Rows.Add(r);
 
        r = null;
 
        return dt;
    }
    
    
    
    protected string strIDs = "";
 
    protected int iCheckBoxes = 0;
    
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
        if (Page.IsPostBack == true) return;
        
        DataTable dt = GetDataTable();
 
        dgTest.DataSource = dt;
        dgTest.DataBind();
    }
 
    protected void dgTest_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
 
        CheckBox chkBoxColumn2_t = e.Item.FindControl("chkBoxColumn2") as CheckBox;
        
        strIDs += "cbQ1[" + iCheckBoxes.ToString() + "] = '" + chkBoxColumn2_t.ClientID.ToString() + "';\r\n";
 
        iCheckBoxes++;
    }
 
    protected void btnContinue_Click(object sender, EventArgs e)
    {
        // Do server side checking in case someone spoofed your page or used FireBug to circumvent client checking.
        bool blnOneChecked = false;
        
        foreach (DataGridItem dgi in dgTest.Items)
        {
            CheckBox chkBoxColumn2_t = dgi.FindControl("chkBoxColumn2") as CheckBox;
            if (chkBoxColumn2_t.Checked == true) blnOneChecked = true;
            if (blnOneChecked == true) break;
        }
 
        if (blnOneChecked == false)
        {
            Response.Write("One wasn't checked.");
            return;
        }
        
        Response.Write("Worked!");
    }
 
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
<script type="text/javascript">
    function fncTest() {
    
        var cbQ1 = new Array(<% = iCheckBoxes %>);
    
        <% = strIDs %>
 
        var blnOneChecked = false;
        var cb;
        for(var i = 0; i < cbQ1.length; i++)
        {
            cb = document.getElementById(cbQ1[i]);
            if (cb.checked == true) blnOneChecked = true;
        }
        
        if (blnOneChecked == true) return true;
        
        var dA = document.getElementById('divAlert');
        dA.style.display = 'block';
 
        return false;
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:DataGrid ID="dgTest" runat="server" AutoGenerateColumns="false" OnItemDataBound="dgTest_ItemDataBound">
        <Columns>
            <asp:TemplateColumn HeaderText="Column 1">
                <ItemTemplate>
                    <%# Eval("Test") %>. 
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Column 2">
                <ItemTemplate>
                    <asp:CheckBox ID="chkBoxColumn2" runat="server" Text="Check one of me!" Checked="false" />
                </ItemTemplate>
            </asp:TemplateColumn>
        </Columns>
    </asp:DataGrid>
    
    <div id="divAlert" style="display: none;">
        <span style="color: Red;">Please select an option.</span>
    </div>
    
    <br /><br />
    <asp:Button ID="btnContinue" runat="server" Text="Continue &gt;&gt;" OnClientClick="return fncTest();" OnClick="btnContinue_Click" />
    </div>
    </form>
</body>
</html>

Open in new window

0
itzdarcCommented:
Further testing revealed that the above code doesn't persist past the first postback.  I have added a hidden field and used it to store the javascript code generated in the ItemDataBound event.   It persists properly.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
    
    // Just used to make a temporary record set.
    protected DataTable GetDataTable()
    {
        DataTable dt = new DataTable("Table");
 
        dt.Columns.Add("Test", typeof(string));
 
        DataRow r = dt.NewRow();
        r["Test"] = "1";
        dt.Rows.Add(r);
 
        r = null;
        r = dt.NewRow();
 
        r["Test"] = "2";
        dt.Rows.Add(r);
 
        r = null;
        r = dt.NewRow();
 
        r["Test"] = "3";
        dt.Rows.Add(r);
 
        r = null;
 
        return dt;
    }
    
    
    
    protected string strIDs = "";
    protected string strOtherIDs = "";
 
    protected int iCheckBoxes = 0;
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack == true) return;
        
        DataTable dt = GetDataTable();
 
        dgTest.DataSource = dt;
        dgTest.DataBind();
    }
 
    protected void dgTest_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
 
        CheckBox chkBoxColumn2_t = e.Item.FindControl("chkBoxColumn2") as CheckBox;
        
        strIDs += "cbQ1[" + iCheckBoxes.ToString() + "] = '" + chkBoxColumn2_t.ClientID.ToString() + "';\r\n";
 
        hfOtherIDs.Value = strIDs;
 
        //if (strOtherIDs.Length == 0)
        //    strOtherIDs += chkBoxColumn2_t.ClientID.ToString();
        //else
        //    strOtherIDs += "," + chkBoxColumn2_t.ClientID.ToString();
 
        //hfOtherIDs.Value = strOtherIDs;
 
        iCheckBoxes++;
    }
 
    protected void btnContinue_Click(object sender, EventArgs e)
    {
        // Do server side checking in case someone spoofed your page or used FireBug to circumvent client checking.
        bool blnOneChecked = false;
        
        foreach (DataGridItem dgi in dgTest.Items)
        {
            CheckBox chkBoxColumn2_t = dgi.FindControl("chkBoxColumn2") as CheckBox;
            if (chkBoxColumn2_t.Checked == true) blnOneChecked = true;
            if (blnOneChecked == true) break;
        }
 
        if (blnOneChecked == false)
        {
            Response.Write("One wasn't checked.");
            return;
        }
        
        Response.Write("Worked!");
    }
 
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
<script type="text/javascript">
    function fncTest() {
    
        var cbQ1 = new Array(<% = iCheckBoxes %>);
    
        <% = hfOtherIDs.Value %>
        <% //= strIDs %>
 
        var blnOneChecked = false;
        var cb;
        
        for(var i = 0; i < cbQ1.length; i++)
        {
            cb = document.getElementById(cbQ1[i]);
            if (cb.checked == true) blnOneChecked = true;
        }
        
        if (blnOneChecked == true) return true;
        
        var dA = document.getElementById('divAlert');
        dA.style.display = 'block';
 
        return false;
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:HiddenField ID="hfOtherIDs" runat="server" Value="" />
    
    <asp:DataGrid ID="dgTest" runat="server" AutoGenerateColumns="false" OnItemDataBound="dgTest_ItemDataBound">
        <Columns>
            <asp:TemplateColumn HeaderText="Column 1">
                <ItemTemplate>
                    <%# Eval("Test") %>. 
                </ItemTemplate>
            </asp:TemplateColumn>
            <asp:TemplateColumn HeaderText="Column 2">
                <ItemTemplate>
                    <asp:CheckBox ID="chkBoxColumn2" runat="server" Text="Check one of me!" Checked="false" />
                </ItemTemplate>
            </asp:TemplateColumn>
        </Columns>
    </asp:DataGrid>
    
    <div id="divAlert" style="display: none;">
        <span style="color: Red;">Please select an option.</span>
    </div>
    
    <br /><br />
    <asp:Button ID="btnContinue" runat="server" Text="Continue &gt;&gt;" OnClientClick="return fncTest();" OnClick="btnContinue_Click" />
    </div>
    </form>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.