动态生成了几列模板列,模板列中添加CheckBox
TemplateColumn tm=new TemplateColumn(); 
tm.ItemTemplate=new ColumnTemplateCbox(CheckBoxID); 
DataGridID.Columns.Add(tm);
public class ColumnTemplateCbox : ITemplate 
{
  string s;
  public ColumnTemplateCbox(string str)
  {
    s=str;
  }
  public void InstantiateIn(Control container)       
  {
    CheckBox cbox =new CheckBox();
    cbox.ID=s;
    cbox.Text="";
    container.Controls.Add(cbox);
  }
}我想在DataGrid外点击按钮的时候,取到CheckBox是否选中※使用固定模板列中取控件方法不管用,如
  ((CheckBox)DataGrid.Items[itemsNO].Cells[0].FindControl(CheckBoxID)).Checked
  这样提示为找不到控件,断点后发现,datagrid中动态生成的模板列已经没有了。
※如果在pageload里重新生成,模板列到是有了,但是用户选择的状态就没了问题:如何能在DataGrid外的点击事件里取得动态模板列中控件的属性
万万分感谢

解决方案 »

  1.   

    我觉得这个不太好弄,不知道别人有没有好的办法,我以前做这种动态添加的组件都是用ViewStat去记录状态,回传后获取状态,然后在处理,但是我的比较简单,2、3个ViewState就能解决问题。
    另外不明白为什么要动态添加模板列
      

  2.   

    试试我的方法:在模板列中分别加入checkbox:一个名为:全选,在headertemplate,另一个为:chkSelect,在itemtemplate中,
    代码://定义的选择框的方法:public void SelectAll(object sender,System.EventArgs e)
    {
    CheckBox chkSelectAll=(CheckBox)sender;
    if(chkSelectAll.Text=="全选")
    {
    foreach(DataGridItem dgi in this.dg.Items)
    {
    CheckBox chkSelect=(CheckBox)d       gi.FindControl("chkSelect");
    chkSelect.Checked=chkSelectAll.Checked;
    }
    }
    }
    删除按钮的事件是: private void btnDeleteNews_Click(object sender, System.EventArgs e)
    {
    int deleteCount=0; foreach(DataGridItem dgi in this.dg.Items)
    {
    CheckBox chkSelect=(CheckBox)dgi.FindControl("chkSelect");
    if(chkSelect.Checked)
    {
    if(this.dg.Items.Count==1)
    {
    if(this.dg.CurrentPageIndex!=0)
    {
    this.dg.CurrentPageIndex=this.dg.CurrentPageIndex - 1; this.lbSum.Text=Convert.ToString(Convert.ToInt32(this.lbSum.Text)-1);
    this.lbCurPage.Text=Convert.ToString(Convert.ToInt32(this.lbCurPage.Text)-1);
    }
    } string  pid=dgi.Cells[0].Text;
    string strSql="delete from productshow where pid=" + pid; this.ExecuteSql(strSql); deleteCount++;
    }
    } if(deleteCount!=0)
    {
    Response.Write("<script languange=javascript>alert('删除成功!');</script>"); string strSql="select * from productshow";
    this.BindDataToDataGrid(strSql); //定义的分页:this.TurnPageButtonEnabled(); this.lbSum.Text=this.dg.PageCount.ToString();
    }
    else
    {
    Response.Write("<script language=javascript>alert('请先选择要删除的项!');</script>");
    }
    }
      

  3.   

    string uid=dgRetireInfo.Items[i].UniqueID+":";//


    if (Request.Form[uid+strCheckBox1]=="on")
    {
    BackCondition=2;
    }
    else
    {
    BackCondition=1;
    }
      

  4.   

    radiobutton的时候Request.Form[uid+groupName]返回选中的buttonID