Where are the DataValueField values for a CheckBoxList stored?

前端 未结 10 2075
旧时难觅i
旧时难觅i 2020-12-16 06:42

I have this CheckBoxList on a page:



        
10条回答
  •  抹茶落季
    2020-12-16 07:04

    Swapna, Your answer absolutely works. So in order to check if the check box in the ASP.Net Checkboxlist is checked and then accumulate the list as a comma-delimited string, you can do as

    C# Code-behind

    ChkboxList1.DataSource = dsData;
    ChkboxList1.DataTextField = "your-display-column-name";
    ChkboxList1.DataValueField = "your-identifier-column-name";
    ChkboxList1.DataBind();
    
    foreach (ListItem li in ChkboxList1.Items)
    {
        li.Attributes.Add("DataValue", li.Value); 
    }
    

    Then in Javascript do

    var selValues = "";
    var ChkboxList1Ctl = document.getElementById("ChkboxList1");
    var ChkboxList1Arr = null;
    var ChkboxList1Attr= null;
    
    if (ChkboxList1Ctl != null)
    {
       ChkboxList1Arr = ChkboxList1Ctl.getElementsByTagName("INPUT");
       ChkboxList1Attr = ChkboxList1Ctl.getElementByTagName("span");
    }
    if (ChkboxList1Arr != null)
    {
        for (var i = 0; i < ChkboxList1Arr.length; i++)
        {
           if (ChkboxList1Arr[i].checked)
               selValues += ChkboxList1Attr[i].getAttribute("DataValue") + ",";
        }
        if (selValues.length > 0)
           selValues = selValues.substr(0, selValues.length - 1);
    }
    

提交回复
热议问题