Where are the DataValueField values for a CheckBoxList stored?

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

I have this CheckBoxList on a page:



        
相关标签:
10条回答
  • 2020-12-16 07:00

    Removing <pages controlRenderingCompatibilityVersion="3.5"> caused an issue in some old code I was working on. I was getting this error. Once I got that error I realized it was too risky of a change.

    To fix this, I ended up extending CheckBoxList and overriding the Render method to add an additional attribute containing the value to each list item:

    public class CheckBoxListExt : CheckBoxList
    {
        protected override void Render(HtmlTextWriter writer)
        {
            foreach (ListItem item in this.Items)
            {
                item.Attributes.Add("data-value", item.Value);
            }
    
            base.Render(writer);
        }
    }
    

    This will render the data-value attribute on the outer span tag.

    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-12-16 07:04

    I've searched for this before and you may be close to little luck. I think i remember seeing that there isn't a checkbox list item for javascript, so it doesn't understand it. You'll have to search for items on the page that have the type of a check box and test to see what group (i think that's the property) that it belongs too.

    I'll search my code and see if I can find what I did...


    Of course, I can't find what I did.

    Why are you doing it client side in javascript? Why don't you do some AJAX and control everything server side?

    0 讨论(0)
  • 2020-12-16 07:08
    <body>
         <form id="form1" runat="server">
              <div>
                   <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataTextField="tx" DataValueField="vl">
                   </asp:CheckBoxList>
              </div>
              <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" /> 
         </form>
    </body>
    <script type="text/javascript">
    
    function Button1_onclick() 
    {
    var itemarr = document.getElementById("CheckBoxList1").getElementsByTagName("span");
    var itemlen = itemarr.length;
         for(i = 0; i <itemlen;i++)
         {
              alert(itemarr[i].getAttribute("dvalue"));
         }
    return false;
    }
    
    
    </script>
    

    Code

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("tx");
            dt.Columns.Add("vl");
            DataRow dr = dt.NewRow();
            dr[0] = "asdas";
            dr[1] = "1";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "456456";
            dr[1] = "2";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "yjryut";
            dr[1] = "3";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "yjrfdgdfgyut";
            dr[1] = "3";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr[0] = "34534";
            dr[1] = "3";
            dt.Rows.Add(dr);
            CheckBoxList1.DataSource = dt;
            CheckBoxList1.DataBind();
            foreach (ListItem li in CheckBoxList1.Items)
            {
                li.Attributes.Add("dvalue", li.Value);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-16 07:09

    Stored in ViewState, you cannot access them on the client without some hacking.

    0 讨论(0)
  • 2020-12-16 07:13

    I finally have the answer I've been looking for!

    The asp.net CheckboxList control does in fact render the value attribute to HTML - it has been working for me in a Production site for over a year now! (We ALWAYS have EnableViewState turned off for all our sites and it still works, without any tweaks or hacks)

    However, all of a sudden, it stopped working one day, and our CheckboxList checkboxes no longer were rendering their value attribute in HTML! WTF you say? So did we! It took a while to figure this out, but since it had been working before, we knew there had to be a reason. The reason was an accidental change to our web.config!

    <pages controlRenderingCompatibilityVersion="3.5">
    

    We removed this attribute from the pages configuration section and that did the trick!

    Why is this so? We reflected the code for the CheckboxList control and found this in the RenderItem method:

    if (this.RenderingCompatibility >= VersionUtil.Framework40)
    {
        this._controlToRepeat.InputAttributes.Add("value", item.Value);
    }
    

    Dearest dev brothers and sisters do not despair! ALL the answers I searched for here on Stackexchange and the rest of the web gave erroneous information! As of .Net 4.0 asp.net renders the value attribute of the checkboxes of a CheckboxList:

    <input type="checkbox" value="myvalue1" id="someid" />
    

    Perhaps not practically useful, Microsoft gave you the ability to add a "controlRenderingCompatibilityVersion" to your web.config to turn this off by setting to a version lower than 4, which for our purposes is completely unnecessary and in fact harmful, since javascript code relied on the value attribute...

    We were getting chk.val() equal to "on" for all our checkboxes, which is what originally alerted us to this problem in the first place (using jquery.val() which gets the value of the checkbox. Turns out "on" is the value of a checkbox that is checked... Learn something every day).

    0 讨论(0)
提交回复
热议问题