ASP.net cannot set the value of flags!

CheckBox newBox = new CheckBox();
newBox.Text = dtCommon[i].userName;
newBox.CssClass = "cbox";
newBox.Attributes["value"] = dtCommon[i].id.ToString();
ApprovalSelectPanel.Controls.Add(newBox);

Refers to:

<input id="ctl00_mainContent_ctl00" type="checkbox" name="ctl00$mainContent$ctl00" checked="checked" />

How can I get the value attribute? My jQuery needs to access it!

+3
source share
4 answers

I bet you set the attribute, but in the containing range (see one element).

Instead, you want to use the InputAttributes property :

newBox.InputAttributes["value"] = dtCommon[i].id.ToString();
+9
source
 newBox.Attributes.Add("yourAttributeName", "yourAttributeValue");

EDIT: Sorry I forgot that the checkboxes are a little different, so you need to do:

newBox.InputAttributes.Add("yourAttributeName", "yourAttributeValue");

If you want to access spanaround the checkbox control, this will work the original or you can do:

newBox.LabelAttributes.Add("yourAttributeName", "yourAttributeValue");
+1
source

newBox.Attributes.Add("Value", dtCommon[i].id.ToString());

0

If you need to keep the value in this checkbox, I recommend using something other than value, such as "MyValue". You can still get this "MyValue" using the .Attributes method later in your processing. In jquery you can use .attr ('MyValue') to get the value.

0
source

All Articles