DataGrid动态模板列中CheckBox取值问题,
代码如下:private void Page_Load(object sender, System.EventArgs e)
{
CreateTemplateColumn();
if (!Page.IsPostBack )
{
BindData(0);
}
}
private void CreateTemplateColumn()
{
DataSet ds=new DataSet();
ds = new OperationDA().GetAllOperation();
for (int i=0;i<ds.Tables[0].Rows.Count ;i++)
{
TemplateColumn tc=new TemplateColumn();
tc.HeaderText =ds.Tables[0].Rows[i].ItemArray[2].ToString();
tc.ItemTemplate =new CTemplateColumnCheckBox(ds.Tables[0].Rows[i].ItemArray[0].ToString(),ds.Tables[0].Rows[i].ItemArray[2].ToString());
PrivilegeList.Columns.Add(tc);
}
}
public class CTemplateColumnCheckBox:ITemplate
{
private string cbxId;
private string cbxName;
public CTemplateColumnCheckBox(string id,string name)
{
cbxId=id;
cbxName=name;
}
public void InstantiateIn(Control container)
{
CheckBox cbx = new CheckBox();
cbx.EnableViewState =true;
cbx.DataBinding += new EventHandler(this.OnDataBinding);
container.Controls.Add(cbx);
}
public void OnDataBinding(object sender, EventArgs e)
{
CheckBox cbx = (CheckBox) sender;
DataGridItem container = (DataGridItem) cbx.NamingContainer;
cbx.Checked =System.Boolean.Parse(((DataRowView)container.DataItem)[cbxName].ToString()) ;
}
}
private void BindData(int pageIndex)
{
PrivilegeDA pda=new PrivilegeDA();
DataSet ds=new DataSet();
GetTiaoJianSql();
ds=pda.FB_GetAllResourceAndPrivilege (pageIndex,PrivilegeList.PageSize,resourceTypeId,resourceId,out pageCount);
this.PrivilegeList.VirtualItemCount=pageCount;
this.PrivilegeList.DataSource=ds;
this.PrivilegeList.DataBind();
}
其中checkbox都是动态生成的,但在值改变后,我不能取到它的值,而且也找不到,请大虾们帮帮忙 --------------------编程问答-------------------- mark下 --------------------编程问答-------------------- Mark --------------------编程问答-------------------- mark就可以了
--------------------编程问答--------------------
--------------------编程问答-------------------- 一个简单的方法:运行页面后“查看源代码”,看看checkbox的id是什么规律,一般是加个前缀,代码中进行相应修改即可
/****DataGrid动态列****/
public class ColumnTemplate1 : ITemplate
{
//数据列
private string colname;
public ColumnTemplate1(string ColName)
{
colname=ColName;
}
public string ColName
{
set{colname=value;}
get{return colname;}
}
public void InstantiateIn(Control container)
{
LiteralControl l = new LiteralControl();
l.DataBinding += new EventHandler(this.OnDataBinding);
//数据绑定
container.Controls.Add(l);
//为模板列加入LiteralControl
}
public void OnDataBinding(object sender, EventArgs e)
{
LiteralControl l = (LiteralControl) sender;//LiteralControl发送绑定请求
DataGridItem container = (DataGridItem) l.NamingContainer;
l.Text = ((DataRowView)container.DataItem)[colname].ToString();//绑定stuname字段
}
}
public class ColumnTemplate2 : ITemplate
{
//编辑列
private string idname;
private string colname;
public ColumnTemplate2 (string ID,string Colname)
{
colname=Colname;
idname=ID;
}
public void InstantiateIn(Control container)
{
CheckBox chek = new CheckBox();
chek.ID=idname;
chek.TextAlign=TextAlign.Left;
chek.Text=colname;
container.Controls.Add(chek);
}
}
补充:.NET技术 , ASP.NET