首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

怎么操作gridview中动态生成的模板列checkbox

2012-01-20 
如何操作gridview中动态生成的模板列checkbox?我在gridview中动态生成模板列,类型为checkbox,然后把后台数

如何操作gridview中动态生成的模板列checkbox?
我在gridview中动态生成模板列,类型为checkbox,然后把后台数据库表中的数据对其进行绑定,代码如下:
              private   void   setTemplateFields()
                {
                        TemplateField   templateField1   =   new   TemplateField();
                        templateField1.ItemTemplate   =   new   GridViewTemplate(DataControlRowType.DataRow,   "列1 ");

                        TemplateField   templateField2   =   new   TemplateField();
                        templateField2.ItemTemplate   =   new   GridViewTemplate(DataControlRowType.DataRow,   "列2 ");
                        //将字段添加到GridView
                        GridView1.Columns.Add(templateField1);
                        GridView1.Columns.Add(templateField2);                        
                }

                //样板类产生器,以创建样板字段
                public   class   GridViewTemplate   :   ITemplate
                {
                        private   DataControlRowType   templateType;
                        private   string   columnName;

                        public   GridViewTemplate(DataControlRowType   type,   string   colname)
                        {
                                templateType   =   type;
                                columnName   =   colname;
                        }

                        public   void   InstantiateIn(System.Web.UI.Control   container)
                        {
                                if   (templateType   ==   DataControlRowType.DataRow)
                                {
                                        //创建样板字段外观
                                        //TextBox   txtEmployeeID   =   new   TextBox();


                                        //RadioButton   txtFirstName   =   new   RadioButton();
                                       
                                        //指定样板字段的数据绑定事件
                                        switch   (columnName)
                                        {
                                                case   "列1 ":
                                                        CheckBox   cbCol1   =   new   CheckBox();
                                                        cbCol1.DataBinding   +=   new   EventHandler(this.cbCol1_DataBinding);
                                                        container.Controls.Add(cbCol1);
                                                        break;
                                                case   "列2 ":
                                                        CheckBox   cbCol2   =   new   CheckBox();
                                                        cbCol2.DataBinding   +=   new   EventHandler(this.cbCol2_DataBinding);
                                                        container.Controls.Add(cbCol2);
                                                        break;  
                                        }
                                }
                        }

                        //样板的DataBind


                        private   void   cbCol1_DataBinding(Object   sender,   EventArgs   e)
                        {
                                CheckBox   cbx   =   (CheckBox)sender;
                                //取得GridViewRow(包含CheckBox控件)
                                GridViewRow   row   =   (GridViewRow)cbxCity.NamingContainer;
                                //进行数据绑定
                                cbx.Text   =   DataBinder.Eval(row.DataItem,   "col1 ").ToString();                                
                        }
                        private   void   cbCol2_DataBinding(Object   sender,   EventArgs   e)
                        {
                                CheckBox   cbx   =   (CheckBox)sender;
                                //取得GridViewRow(包含CheckBox控件)
                                GridViewRow   row   =   (GridViewRow)cbxCity.NamingContainer;
                                //进行数据绑定
                                cbx.Text   =   DataBinder.Eval(row.DataItem,   "col2 ").ToString();
                        }
                }
              protected   void   Page_Load(object   sender,   EventArgs   e)
                {
                        if   (!IsPostBack)
                        {
                                setTemplateFields();
                        }
              }
现在,数据能够正确显示。我要做的是,(1)根据数据库表中的值确定每一个对应的checkbox的enable的值,(2)获取每一个checkbox.checked的值,根据获得的值对后台数据库进行操作。在GridView1_RowDataBound中通过命令(CheckBox)e.Row.Cells[0].FindControl( "cbCol1 ")总是得到NULL。各位大侠,怎么样才能完成我的上两个需求?

[解决办法]
If(e.Row.RowType == DataControlRowType.DataRow){
((CheckBox)e.Row.Cells[0].FindControl( "cbCol1 ").checked=True;


}
[解决办法]
呵呵。。。DataKeys里面就可以绑啊
[解决办法]
protected void btnDel_Click(object sender, EventArgs e)
{
int intRowCount = this.GridView1.Rows.Count;
string strInfo = string.Empty;

for (int i = 0; i < intRowCount; i++)
{
CheckBox chk = (CheckBox) this.GridView1.Rows[i].Cells[0].FindControl( "chkDelete ");
if (chk.Checked)
{
this.GridView1.Rows[i].Visible = false;
strInfo += this.GridView1.Rows[i].Cells[1].Text + "被选中. " + " <br> ";
}
}

this.lblInfo.Text = strInfo;
}
[解决办法]
将GridView1_RowDataBound 的事件换成GridView1_ItmCreated事件,就能取到值了~~~~~~~
[解决办法]
那你可以把无关的列隐藏不就好了。还有你的checkbox列的摸版列是固定的还是动态生成出来的。
[解决办法]
(CheckBox)e.Row.Cells[0].FindControl( "cbCol1 ")

热点排行