RadioButtonList在DataList或者GridView里什么取值
本人自学所以基础很差- -~~
[解决办法]
页面应该看得懂吧,后台注释如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//初始化一个Datatable
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("name",typeof(string)),
new DataColumn("age",typeof(string))
});
DataRow dr = null;
dr = dt.NewRow();
dr["name"] = "guwei";
dr["age"] = "30";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["name"] = "google";
dr["age"] = "28";
dt.Rows.Add(dr);
//DataList绑定这个Datatable
this.DataList1.DataSource = dt;
this.DataList1.DataBind();
}
}
//这个而是DataList1控件的ItemDataBound事件
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
//这里可以在DataList1绑定后,获取每一个RadioButtonList
RadioButtonList radioButtonList = e.Item.FindControl("RadioButtonList1") as RadioButtonList;
}
//这里是一个按钮,随意点击遍历这个DataList1的Item,来获取所选的值
protected void Button1_Click(object sender, EventArgs e)
{
foreach (DataListItem item in DataList1.Items)
{
RadioButtonList radioButtonList = item.FindControl("RadioButtonList1") as RadioButtonList;
string value = radioButtonList.SelectedValue;
}
}