2011-03-09 74 views
3
CheckBox newBox = new CheckBox(); 
newBox.Text = dtCommon[i].userName; 
newBox.CssClass = "cbox"; 
newBox.Attributes["value"] = dtCommon[i].id.ToString(); 
ApprovalSelectPanel.Controls.Add(newBox); 

呈現爲:ASP.net無法設置複選框的值!

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

我怎樣才能得到一個值的屬性?我的JQuery需要訪問這個!

回答

9

我敢打賭,它是設置屬性,但在包含跨度(查找一個元素)。

你想使用InputAttributes屬性來代替:

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

謝謝!你是對的! – 2011-03-09 17:12:07

0

你可以嘗試newBox.Attributes.Add("Value", dtCommon[i].id.ToString());

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

編輯:對不起,我忘了複選框行爲有點差異,所以你需要做的:如果你要訪問的span各地的複選框

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

控制原來的工作或你可以這樣做:

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

仍然沒有渲染值屬性 – 2011-03-09 17:07:34

0

如果您需要在複選框上存儲值,我建議使用除「值」之外的其他值,例如「MyValue」。您仍然可以在處理中稍後使用.Attributes方法獲取此「MyValue」。在jQuery中,你可以使用.attr('MyValue')來獲取值。