开发者

ASP.net can't set checkboxes value!

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

Renders as:

<input id="ctl00_mainContent_ctl开发者_运维技巧00" type="checkbox" name="ctl00$mainContent$ctl00" checked="checked" />

How can I get a value attribute on? My JQuery needs to access this!


I bet you it is setting the attribute, but on the containing span (look up one element).

You want to use the InputAttributes property instead:

newBox.InputAttributes["value"] = dtCommon[i].id.ToString();


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

EDIT: Sorry I forgot checkboxes act a little diff so you need to do:

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

If you want to access the span around the checkbox control the original would work or you could do:

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


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


If you need to store a value on the checkbox, I recommend using something besides value, such as "MyValue". You can still get this "MyValue" using the .Attributes method later in your processing. In jquery, you could use the .attr('MyValue') to obtain the value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜