我有占位符,里面有我的面板。在内部面板我有桌子和内部表格我在每一行都有radiobuttonlist。我如何循环遍历每个表格行和fins radiobuttonlist选择的项目?。请帮助。
代码如下:
PlaceHolder1.Controls.Add(panel);
Table table = new Table();
foreach (DataRow row in dt.Rows)
{
var rd1 = new RadioButtonList();
TableRow tRow1 = new TableRow();
TableRow tRow2 = new TableRow();
TableCell tCell1 = new TableCell();
TableCell tCell2 = new TableCell();
TableCell tCell3 = new TableCell();
tRow1.Cells.Add(tCell1);
tRow2.Cells.Add(tCell2);
tCell2.Controls.Add(rd1);
table.Rows.Add(tRow1);
table.Rows.Add(tRow2);
panel.Controls.Add(table);
}
答案 0 :(得分:0)
您可以尝试以下代码。
foreach(Control ctrl in PlaceHolder1.Controls)
{
if(ctrl is Panel)
{
foreach(Control ctrlPnl in ctrl.Controls)
{
if(ctrlPnl is Table)
{
foreach(TableRow row in ((Table)ctrlPnl).Rows)
{
foreach(TableCell cel in row.Cells)
{
foreach(Control ctrlCell in cel.Controls)
{
if(ctrlCell is RadioButtonList)
{
//Do your stuff here
}
}
}
}
}
}
}
}
答案 1 :(得分:0)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PlaceHolder1.Controls.Add(GetRadioButtonList("RadioButtonList1"));
}
private RadioButtonList GetRadioButtonList(string ControlID)
{
RadioButtonList rbl = new RadioButtonList();
rbl.Items.AddRange(
new ListItem[] {
new ListItem("Item 1", "1"),
new ListItem("Item 2", "2"),
new ListItem("Item 3", "3"),
new ListItem("Item 4", "4"),
new ListItem("Item 5", "5"),
new ListItem("Item 6", "6"),
new ListItem("Item 7", "7"),
new ListItem("Item 8", "8"),
new ListItem("Item 9", "9"),
new ListItem("Item 10", "10")
});
rbl.ID = ControlID;
return rbl;
}
protected void Button1_Click(object sender, EventArgs e)
{
RadioButtonList rbl = (RadioButtonList)PlaceHolder1.FindControl("RadioButtonList1");
Response.Write(rbl.SelectedValue);
}
}
Use ->PlaceHolder1.FindControl("RadioButtonList1");