Tuesday, August 09, 2011

Create nested checkboxes in asp.net.

Posted by Rahul Kharde at 2:26 AM
This tutorial will show you how to display data in CheckBoxList in nested format using Repeater control .NET. CheckBoxList Control Nested within my Repeater Control. I needed to get the checked values of the items in each iteration of the Repeater Control. The checkboxlist was located in the ItemTemplate in the Repeater Control. In the each Iteration set the javascript function on the click on the Chekbox in the Repeater1_ItemDataBound control

HTML

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

    <script language="javascript" type="text/javascript">   
function Check(Ch,Chl)   
{
    var chkList2=document.getElementById(Chl);
    var checkbox2 = chkList2.getElementsByTagName("input");
    for (var i=0;i
    {
        if (document.getElementById(Ch).checked)
            checkbox2[i].checked=true;
        else
            checkbox2[i].checked=false;
    }
}

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
            <ItemTemplate>
                <div style="font-family: Arial Verdana; size: 10px;">
                    <b>
                        <asp:CheckBox ID="CheckBox1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Name")%>' />
                    </b>
                    <asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("ID") %>' />
                    <ul style="font-family: Arial Verdana; size: 8px;">
                        <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                            <asp:ListItem>Checkbox 1</asp:ListItem>
                            <asp:ListItem>Checkbox 2</asp:ListItem>
                            <asp:ListItem>Checkbox 3</asp:ListItem>
                        </asp:CheckBoxList>
                    </ul>
                </div>
            </ItemTemplate>
            <SeparatorTemplate>
                <hr />
            </SeparatorTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>

Code

        protected void Page_Load(object sender, EventArgs e)
        {
            this.BindRepeater();
        }

        private void BindRepeater()
        {
            DataSet _Objds = new DataSet();
            _Objds.Tables.Add("ds");
            _Objds.Tables[0].Columns.Add("ID");
            _Objds.Tables[0].Columns.Add("Name");


            for (int i = 1; i < 5; i++)
            {
                DataRow dr = (DataRow)_Objds.Tables[0].NewRow();
                dr["ID"] = i.ToString();
                dr["Name"] = "checkbox_" + i.ToString();
                _Objds.Tables[0].Rows.Add(dr);
            }
            Repeater1.DataSource = _Objds.Tables[0];
            Repeater1.DataBind();
        }
        public void Repeater1_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                CheckBox Ch = (CheckBox)e.Item.FindControl("CheckBox1");
                CheckBoxList Chl = (CheckBoxList)e.Item.FindControl("CheckBoxList1");
                Ch.Attributes.Add("OnClick", "Javascript:Check('" + Ch.ClientID + "','" + Chl.ClientID + "')");
            }
        }



If you enjoyed this post and wish to be informed whenever a new post is published, then make sure you subscribe to my regular Email Updates. Subscribe Now!


Kindly Bookmark and Share it:

YOUR ADSENSE CODE GOES HERE

0 comments:

Have any question? Feel Free To Post Below:

 

Popular Posts

Recent Comments

© 2011. All Rights Reserved | Help to understand .Net | Template by Blogger Widgets

Home | About | Top