动态生成了几列模板列,模板列中添加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外的点击事件里取得动态模板列中控件的属性
万万分感谢
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外的点击事件里取得动态模板列中控件的属性
万万分感谢
另外不明白为什么要动态添加模板列
代码://定义的选择框的方法: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>");
}
}
if (Request.Form[uid+strCheckBox1]=="on")
{
BackCondition=2;
}
else
{
BackCondition=1;
}