Happy Pig's - 哼哼
开开心心每一天,勤勤奋奋过日子。
在DataList中找到自己添加的控件有两种方式,以RadioButtonList控件为例:

1.在DataList的ItemDateBound或是CreatDataBound事件中写
--rbID是RadioButtonList 控件的ID属性
Protected void DataList1_ItemDataBound(object sender,DataListItemEventArgs e)
{
   if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
     {
       RadioButtonList rbl1 = e.Item.FindControl("rbID") as RadioButtonList ;//找到RadioButtonList 
    }
}
2如果在其他地方,且DataList控件在page_load事件中绑定了,一定要保证绑定内容在
if(!IsPostBack)
{
  //绑定BindDataList();
}
因为在其他地方的可以引起页面回发得控件会在引起页面刷新,重新绑定RadioButtonList 
这样就获取不了RadioButtonList 选中的值了
代码可以这样写:
foreach(DataListItem item in DataList1.Items)
{
   RadioButtonList  rbl2 = ( RadioButtonList )item.FindControl("rbId");
   if(rbl2.SelectedIndex !=-1)
    {
     //你自己的操作
    }
}

posted on 2007-09-13 12:32  boulder  阅读(1298)  评论(0)    收藏  举报