Click here to Skip to main content
15,906,558 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Dear All,

I have a scenario where I am using checkboxes inside a DataList.
I have a requirement that atleast one checkbox should be always selected.
I am trying to achieve the same using javascript but it is not working.
Can any body help me?

Below is the sample code:

aspx file :

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="ESS.Components.test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>

<body>
<script type="text/javascript" language="javascript">

    function openWin2(u, k) {
        window.open("../Pages/ELC_OrgLevelWiseSignatoryConfigurationPopUp.aspx" + u + " &Mode=" + k, "composerwindow", "toolbar=no,width=850,height=550,status=no,scrollbars=yes,resize=no,menubar=no");
    }

    function CheckBoxClicked(chk) {
        debugger;
        var dl = document.getElementById("<%=DataList1.ClientID%>"); //Replace DatList1 with your DataList ID
        var inputs = dl.getElementsByTagName("input");
        var checked;

        checked = false;
        for (var i = 0; i < inputs.length; i++) {

            if (inputs[i].type == "checkbox")
            {
                if (inputs[i].id == chk.id)
                {
                    if (inputs[i].checked == true);
                    {
                        checked = true;
                        break;
                    }
                }
            }
        }
//        return true;
        if (checked == false) {
            alert("aaaa");
            return false;
        }
        else {
            return false;
        }
    }

</script>

    <form id="form1" runat="server">
    <div>
        <asp:DataList ID="DataList1" runat="server">
            <ItemTemplate>
                <asp:checkbox ID="Checkbox1" runat="server"
                 Text='<%#Eval("orglvl") %>' AutoPostBack="true" Checked="true"
                oncheckedchanged="Checkbox1_CheckedChanged" onclick = "CheckBoxClicked(this)"></asp:checkbox>
            </ItemTemplate>
        </asp:DataList>
    </div>
    </form>
</body>
</html>



cs file :

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace ESS.Components
{
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            filldatalist();
        }

        private void filldatalist()
        { 
            DataTable dt = new DataTable();
            dt.Columns.Add("orglvl", typeof(int));
            DataRow row;
            for(int i=1; i<=10; i++)
            {
                row = dt.NewRow();
                row["orglvl"] = i;
                dt.Rows.Add(row);
            }
            DataList1.DataSource = dt;
            DataList1.DataBind();
        }

        protected void Checkbox1_CheckedChanged(object sender, EventArgs e)
        {
            
        }
    }
}



Regards,
Gopal
Posted
Updated 3-Jan-12 17:57pm
v2
Comments
Karthik Harve 3-Jan-12 23:58pm    
[Edit] pre tags added.
Sergey Alexandrovich Kryukov 4-Jan-12 0:07am    
Not a question. (I don't count "Can anybody help me"). Please explain things properly. "Not working" is not an issue report. Start with what you wanted to achieve...
--SA

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900