Where are the DataValueField values for a CheckBoxList stored?

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

I have this CheckBoxList on a page:



        
10条回答
  •  眼角桃花
    2020-12-16 07:00

    Removing 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.

提交回复
热议问题