2013-09-10 37 views

回答

1

item.ItemType是一個枚舉。類型將永遠HtmlInputRadioButton

public enum ListItemType 
    { 
    Header, 
    Footer, 
    Item, 
    AlternatingItem, 
    SelectedItem, 
    EditItem, 
    Separator, 
    Pager, 
    } 

相反,代碼應該是這樣的 -

void Item_XXXX(Object sender, DataListItemEventArgs e) 
{ 
    if (e.Item.ItemType == ListItemType.Item || 
     e.Item.ItemType == ListItemType.AlternatingItem) 
    { 
     // Make sure MyRadioButtonId is an ID of HtmlInputRadioButton 
     var htmlInputRadioButton = e.Item.FindControl("MyRadioButtonId") 
      as HtmlInputRadioButton; 
    } 
} 
1

最好的辦法是:

var radio = item as RadioButton; 
if(null != radio) 
{ 
    // It's a radio button! 
    // The "as" keyword will return null if the cast fails 
} 

或者,你可以使用更清晰

if(item is RadioButton) 
{ 
    var radio = (RadioButton)item; 
} 

但是,這導致兩個演員,效率較低。

+0

我想我的問題再一次,我是在網絡控制。這不是解決。 – JoJo

+0

檢查贏的更新 - 這應該有助於指導你。 –

+0

好吧我真的很想避免使用findcontrol ..我可能只是切換到jQuery來驗證測驗..如果這是必須的。 – JoJo

相關問題