各位大侠帮帮忙啊,我做了个动态添加模板列的程序代码如下,但是现在有两个问题,我怎么也取不到动态添加的模板列中的控件ID,用以下两种方法都取不到:radbtn 总是null,
方法一: int nowcell = 2;//当前列表固定已有列数-1
for (int i = 0; i < dvItemList.Rows.Count; i++)
{
nowcell += 1;
RadioButton radbtn = (RadioButton)dvItemList.Rows[i].Cells[nowcell].FindControl ("dvItemList_ctl02_rad122");
}
方法二:
object radbtn = Request.Form["dvItemList_ctl02_rad122"];html部分:
<td valign="top" id="grid">
<asp:GridView ID="dvItemList" runat="server" >
<Columns>
</Columns>
</asp:GridView>
</td>cs部分: dvItemList.DataKeyNames = new string[] { "PJ_ID" };
dvItemList.AutoGenerateColumns = false;
dvItemList.PageSize = 15;
dvItemList.Width= new Unit("100%");
dvItemList.CssClass = "GridView";
TemplateField field0 = new TemplateField();
field0.HeaderText = "序号";
//Label lbl = new Label();
//int mm =dvItemList.PageIndex * dvItemList.PageSize + dvItemList.Rows.Count +1;
//lbl.Text = mm.ToString();
//field0.ItemTemplate.InstantiateIn(lbl);
//field0.ItemTemplate.InstantiateIn =
//field0.ItemTemplate = new MyTemplate(//new GridViewTemplate(DataControlRowType.DataRow, "");
field0.HeaderStyle.CssClass = "GridViewHeader";
field0.ItemStyle.CssClass = "GridViewRow";
dvItemList.Columns.Add(field0);
BoundField field1 = new BoundField();
field1.HeaderText = "项目名称";
field1.DataField = "PJ_Name";
field1.HeaderStyle.CssClass = "GridViewHeader";
field1.ItemStyle.CssClass = "GridViewRow";
dvItemList.Columns.Add(field1);
BoundField field2 = new BoundField();
field2.HeaderText = "奖评类别";
field2.DataField = "PJ_TypeCode";
field2.HeaderStyle.CssClass = "GridViewHeader";
field2.ItemStyle.CssClass = "GridViewRow";
dvItemList.Columns.Add(field2); TemplateField tfield;
string radioGN = "";//radio控件所属组,name属性值
for (int i = 0; i < dtexp.Rows.Count; i++)//动态添加模板列
{
radioGN = "radioGN1";
if (!string.IsNullOrEmpty(dtexp.Rows[i]["ExpName"].ToString()))
{
tfield = new TemplateField();//初始化模板列
tfield.HeaderTemplate = new MyTemplate(dtexp.Rows[i]["ExpName"].ToString(), "", DataControlRowType.Header);//MyTemplate 自定义模板
tfield.ItemTemplate = new MyTemplate(dtexp.Rows[i]["ExpID"].ToString(), radioGN, DataControlRowType.DataRow);
tfield.HeaderStyle.CssClass = "GridViewHeader";
tfield.ItemStyle.CssClass = "GridViewRow";
dvItemList.Columns.Add(tfield);
}
} dvItemList.DataSource = groupdt.DefaultView;
dvItemList.DataBind();
-------------------------MyTemplate.cs------------------------------------------------------------------------
/// <summary>
/// 自定义模板类
/// gridview动态添加模板列
/// </summary>
public class MyTemplate : ITemplate
{
private string _colname;
private string _groupname;
private DataControlRowType _rowtype; /// <summary>
/// 构造函数
/// </summary>
/// <param name="colname">列的名字(对应数据库的字段) </param>
/// <param name="groupname">radiobutton所属组名称</param>
/// /// <param name="rowtype">行类型</param>
public MyTemplate(string colname, string groupname,DataControlRowType rowtype)
{
_colname = colname;
_groupname = groupname;
_rowtype = rowtype;
} /// <summary>
/// 实现接口ITemplate的方法
/// </summary>
/// <param name="container"> </param>
/// <res>
/// 1、new一个LiteralControl
/// 2、给LiteralControl加一个绑定事件
/// 3、把该控件加到容器控件集合里
/// </res>
public void InstantiateIn(Control container)
{
switch (_rowtype)
{
case DataControlRowType.Header:
Literal ltr = new Literal();
ltr.Text = _colname;
ltr.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(ltr);
break;
case DataControlRowType.DataRow:
RadioButton rbtn = new RadioButton();
rbtn.ID ="rad" + _colname;
rbtn.GroupName = _groupname;
rbtn.Checked = false;
rbtn.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(rbtn);
break; }
} private void OnDataBinding(object sender, EventArgs e)
{
switch(_rowtype)
{
case DataControlRowType.Header:
Literal lit = (Literal)sender;
GridViewRow containerheader = (GridViewRow)lit.NamingContainer;
lit.Text = _colname;
break;
case DataControlRowType.DataRow:
RadioButton radbtn = (RadioButton)sender;
// 得到radiobutton的容器(父亲)——GridViewRow
GridViewRow container = (GridViewRow)radbtn.NamingContainer;
radbtn.ID ="rad" + _colname;
radbtn.GroupName = _groupname;
radbtn.Checked = false;
break;
}
} }
方法一: int nowcell = 2;//当前列表固定已有列数-1
for (int i = 0; i < dvItemList.Rows.Count; i++)
{
nowcell += 1;
RadioButton radbtn = (RadioButton)dvItemList.Rows[i].Cells[nowcell].FindControl ("dvItemList_ctl02_rad122");
}
方法二:
object radbtn = Request.Form["dvItemList_ctl02_rad122"];html部分:
<td valign="top" id="grid">
<asp:GridView ID="dvItemList" runat="server" >
<Columns>
</Columns>
</asp:GridView>
</td>cs部分: dvItemList.DataKeyNames = new string[] { "PJ_ID" };
dvItemList.AutoGenerateColumns = false;
dvItemList.PageSize = 15;
dvItemList.Width= new Unit("100%");
dvItemList.CssClass = "GridView";
TemplateField field0 = new TemplateField();
field0.HeaderText = "序号";
//Label lbl = new Label();
//int mm =dvItemList.PageIndex * dvItemList.PageSize + dvItemList.Rows.Count +1;
//lbl.Text = mm.ToString();
//field0.ItemTemplate.InstantiateIn(lbl);
//field0.ItemTemplate.InstantiateIn =
//field0.ItemTemplate = new MyTemplate(//new GridViewTemplate(DataControlRowType.DataRow, "");
field0.HeaderStyle.CssClass = "GridViewHeader";
field0.ItemStyle.CssClass = "GridViewRow";
dvItemList.Columns.Add(field0);
BoundField field1 = new BoundField();
field1.HeaderText = "项目名称";
field1.DataField = "PJ_Name";
field1.HeaderStyle.CssClass = "GridViewHeader";
field1.ItemStyle.CssClass = "GridViewRow";
dvItemList.Columns.Add(field1);
BoundField field2 = new BoundField();
field2.HeaderText = "奖评类别";
field2.DataField = "PJ_TypeCode";
field2.HeaderStyle.CssClass = "GridViewHeader";
field2.ItemStyle.CssClass = "GridViewRow";
dvItemList.Columns.Add(field2); TemplateField tfield;
string radioGN = "";//radio控件所属组,name属性值
for (int i = 0; i < dtexp.Rows.Count; i++)//动态添加模板列
{
radioGN = "radioGN1";
if (!string.IsNullOrEmpty(dtexp.Rows[i]["ExpName"].ToString()))
{
tfield = new TemplateField();//初始化模板列
tfield.HeaderTemplate = new MyTemplate(dtexp.Rows[i]["ExpName"].ToString(), "", DataControlRowType.Header);//MyTemplate 自定义模板
tfield.ItemTemplate = new MyTemplate(dtexp.Rows[i]["ExpID"].ToString(), radioGN, DataControlRowType.DataRow);
tfield.HeaderStyle.CssClass = "GridViewHeader";
tfield.ItemStyle.CssClass = "GridViewRow";
dvItemList.Columns.Add(tfield);
}
} dvItemList.DataSource = groupdt.DefaultView;
dvItemList.DataBind();
-------------------------MyTemplate.cs------------------------------------------------------------------------
/// <summary>
/// 自定义模板类
/// gridview动态添加模板列
/// </summary>
public class MyTemplate : ITemplate
{
private string _colname;
private string _groupname;
private DataControlRowType _rowtype; /// <summary>
/// 构造函数
/// </summary>
/// <param name="colname">列的名字(对应数据库的字段) </param>
/// <param name="groupname">radiobutton所属组名称</param>
/// /// <param name="rowtype">行类型</param>
public MyTemplate(string colname, string groupname,DataControlRowType rowtype)
{
_colname = colname;
_groupname = groupname;
_rowtype = rowtype;
} /// <summary>
/// 实现接口ITemplate的方法
/// </summary>
/// <param name="container"> </param>
/// <res>
/// 1、new一个LiteralControl
/// 2、给LiteralControl加一个绑定事件
/// 3、把该控件加到容器控件集合里
/// </res>
public void InstantiateIn(Control container)
{
switch (_rowtype)
{
case DataControlRowType.Header:
Literal ltr = new Literal();
ltr.Text = _colname;
ltr.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(ltr);
break;
case DataControlRowType.DataRow:
RadioButton rbtn = new RadioButton();
rbtn.ID ="rad" + _colname;
rbtn.GroupName = _groupname;
rbtn.Checked = false;
rbtn.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(rbtn);
break; }
} private void OnDataBinding(object sender, EventArgs e)
{
switch(_rowtype)
{
case DataControlRowType.Header:
Literal lit = (Literal)sender;
GridViewRow containerheader = (GridViewRow)lit.NamingContainer;
lit.Text = _colname;
break;
case DataControlRowType.DataRow:
RadioButton radbtn = (RadioButton)sender;
// 得到radiobutton的容器(父亲)——GridViewRow
GridViewRow container = (GridViewRow)radbtn.NamingContainer;
radbtn.ID ="rad" + _colname;
radbtn.GroupName = _groupname;
radbtn.Checked = false;
break;
}
} }
Label lbl = new Label();
int mm =dvItemList.PageIndex * dvItemList.PageSize + dvItemList.Rows.Count +1;
lbl.Text = mm.ToString();
field0.ItemTemplate.InstantiateIn(lbl);
Label lbl = new Label();
int mm =dvItemList.PageIndex * dvItemList.PageSize + dvItemList.Rows.Count +1;
lbl.Text = mm.ToString();
field0.ItemTemplate.InstantiateIn(lbl);