Pages

Monday, 10 October 2011

Get Value of checked item in CheckListBox


Hi all,
try this example to Get Value of checked item in CheckListBox using Javascript
1) Add this Javascript code :-


<script language="javascript" type="text/javascript">
        function CheckItem() {
            var tbl = document.getElementById('<%= CheckBoxList1.ClientID %>').childNodes[0];
            alert(tbl.childNodes[i].childNodes.length);
            for (var i = 0; i < tbl.childNodes.length; i++) {
                for (var k = 0; k < tbl.childNodes[i].childNodes.length; k++) {
                    if (tbl.childNodes[i].childNodes[k].nodeName == "TD") {
                        var currentTD = tbl.childNodes[i].childNodes[k];
                        for (var j = 0; j < currentTD.childNodes.length; j++) {
                            if (currentTD.childNodes[j].nodeName == "SPAN") {
                                var currentSpan = currentTD.childNodes[j];
                                for (var l = 0; l < currentSpan.childNodes.length; l++) {
                                    if (currentSpan.childNodes[l].nodeName == "INPUT" && currentSpan.childNodes[l].type == "checkbox") {
                                        var currentChkBox = currentSpan.childNodes[l];
                                     
                                        if (currentChkBox.checked) {
                                            alert(currentSpan.alt);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
</script>

and HTML Code will be :-



<asp:CheckBoxList ID="CheckBoxList1" runat="server"OnDataBound="CheckBoxList1_DataBound" AppendDataBoundItems="true" >
    </asp:CheckBoxList>
     <input id="Button1" type="button" value="button" onclick="javascript:CheckItem();" />

and in code behind :-



protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            CheckBoxList1.DataSource = BindUsers();
            CheckBoxList1.DataTextField = "User_Name";
            CheckBoxList1.DataValueField = "User_ID";
            CheckBoxList1.DataBind();
        }

    }
    protected void CheckBoxList1_DataBound(object sender, EventArgs e)
    {
        CheckBoxList chkList = (CheckBoxList)(sender);
        foreach (ListItem item in chkList.Items)
        {
            item.Attributes.Add("alt", item.Value);
        }
    }


Hope this helps
Good Luck



No comments:

Post a Comment