webform中datagrid动态添加多个模板列和checkbox列问题!现在都已经动态产生了多个模板列和checkbox列,而且这里列已经从数据库读出响应数据做好了绑定!现在出现了这样的问题!我点“保存”按钮,想把修改checkbox后的状态得到!然后更新数据库!
我的保存事件这样写的! private void lb_save_Click(object sender, System.EventArgs e)
{

//BindGrid(ddl_department.SelectedItem.Value);      (如果不加这个帮定的话,一点保存按钮,那些动态生成的checkbox列就不存在了, 要是加了话,用下面的方法得到的checkbox状态始终是以前的,并不是修改后的状态) for (int i=0;i<=dg.Items.Count-1;i++)
{
string strRight=""; 
for (int j=2;j<dg.Items[0].Cells.Count;j++) //从checkbox列开始检查(第3列开始是动态的checkbox列)
{
 
//string cbx = "dg__ctl"+j.ToString()+"__ctl"+j.ToString();
CheckBox chkTemp = (CheckBox)dg.Items[i].Cells[j].Controls[0];
//CheckBox chkTemp = (CheckBox)dg.Items[i].Cells[j].FindControl(cbx);
if (chkTemp.Checked == false)
{ strRight += "0";
} else
{   
strRight += "1";
}
}
Response.Write(strRight);
Response.Write("<br>"); 修改记录
}

//绑定datagrid
public void BindGrid(string depart)
{ 。
}
 请高手指点,解决了,可以在加分!
[email protected]

解决方案 »

  1.   

    http://community.csdn.net/Expert/topic/3470/3470673.xml?temp=.5829737
      

  2.   

    把获取值的代码写在Page_Load中,IsPostBack为true时执行。
      

  3.   

    //BindGrid(ddl_department.SelectedItem.Value);      (如果不加这个帮定的话,一点保存按钮,那些动态生成的checkbox列就不存在了, 要是加了话,用下面的方法得到的checkbox状态始终是以前的,并不是修改后的状态)
    这句话应该放在Page_Load里面的,这样状态才能保存的
      

  4.   

    为什么要动态加?先加好隐藏起来不行?
    为什么把所有的Cells都找一遍?
    是不是所有的Item里面都有checkbox?
    一个cell的第一个control是literal,所以要找checkbox应该是Controls[1]
      

  5.   

    Page_Load里面也放了BindGrid(ddl_department.SelectedItem.Value);
      

  6.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    namespace TASKMNG.SystemAdmin
    {
    /// <summary>
    /// UserRight 的摘要说明。
    /// </summary> public class UserRight : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DropDownList ddl_department;
    protected System.Web.UI.WebControls.DataGrid dg;
    protected System.Web.UI.WebControls.Label l_error;
    protected System.Web.UI.WebControls.LinkButton lb_save;
    protected SqlConnection myConnection = new SqlConnection();
    public int DepartmentCount;
    public string dpname;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面

    myConnection.ConnectionString=Application["App_ConnectString"].ToString();
    if(!IsPostBack) {  
    BindDepartment();
    BindGrid(ddl_department.SelectedItem.Value);

    }
    } //绑定部门
    public void BindDepartment()
    {
    myConnection.Open();
    SqlCommand myCommand = new SqlCommand("select Department_ID='0',Department_Name='所有员工' union select Department_ID,Department_Name from Dim_Department  ",myConnection);
    SqlDataReader myReader = myCommand.ExecuteReader();
    ddl_department.DataSource = myReader;
    ddl_department.DataTextField="Department_Name";
    ddl_department.DataValueField="Department_ID";
    ddl_department.DataBind();
    myReader.Close();
    myConnection.Close();
              
    } // 动态横向部门标题显示
    public string DepartName() {
    SqlConnection myConnection = new SqlConnection();
    myConnection.ConnectionString=Page.Application["App_ConnectString"].ToString();
    myConnection.Open();
    SqlCommand cmd = new SqlCommand("select Department_Name from Dim_Department",myConnection);
    SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    while (myReader.Read())
    {
    dpname += myReader.GetString(0).ToString()+"-";
    }

    dpname = dpname.Substring(0,dpname.Length-1);
    dpname ="新建任务-"+dpname;
    myReader.Close(); return dpname;

    }
    //统计部门个数已显示动态的checkbox模板列
    public int DepartCount()
    {
    myConnection.Open();
    SqlCommand cmd = new SqlCommand("select Count(*) from Dim_Department ",myConnection);
    DepartmentCount = (int)cmd.ExecuteScalar();
    return DepartmentCount;



    } //绑定datagrid
    public void BindGrid(string depart)
    {   
    DepartCount();
    DepartName();
    string strComm="";
    if (depart=="0")
    {
    strComm="select * from V_UserRight";
    }
    else 
    {
    strComm="select * from V_UserRight where Department_ID='"+depart+"'";
    }
    SqlDataAdapter da = new SqlDataAdapter(strComm,myConnection);
    DataSet ds = new DataSet();

    da.Fill(ds,"UserRight");
    DataView dv = new DataView();
    dv = ds.Tables[0].DefaultView;
    for (int i=0;i<DepartmentCount+1;i++)
    {
    TemplateColumn tc1 = new TemplateColumn();
    tc1.HeaderTemplate = new 
    DataGrid_Template(ListItemType.Header,dpname,i);
    tc1.ItemTemplate = new 
    DataGrid_Template(ListItemType.Item, "Function_Right",i); dg.Columns.Add(tc1); }

    dg.DataKeyField="User_ID";
    dg.DataSource=dv;
    dg.DataBind();
    myConnection.Close();

    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.ddl_department.SelectedIndexChanged += new System.EventHandler(this.ddl_department_SelectedIndexChanged);
    this.lb_save.Click += new System.EventHandler(this.lb_save_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion //部门选择绑定
    private void ddl_department_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    BindGrid(ddl_department.SelectedItem.Value);
    }
             //保存
    private void lb_save_Click(object sender, System.EventArgs e)
    {

    BindGrid(ddl_department.SelectedItem.Value);       for (int i=0;i<=dg.Items.Count-1;i++)
    {
    string strRight=""; 
    for (int j=2;j<dg.Items[0].Cells.Count;j++) //从checkbox列开始检查
    {
     
    string cbx = "dg__ctl"+j.ToString()+"__ctl"+j.ToString();
    //CheckBox chkTemp = (CheckBox)dg.Items[i].Cells[j].Controls[0];
    CheckBox chkTemp = (CheckBox)dg.Items[i].Cells[j].FindControl(cbx);
    if (chkTemp.Checked == false)
    { strRight += "0";
    } else
    {   
    strRight += "1";
    }
    }
    Response.Write(strRight);
    Response.Write("<br>");
    } }
    } // 动态模板类
    public class DataGrid_Template : ITemplate
    {
    ListItemType templateType;
    string columnName;
    int i;

       
    public DataGrid_Template(ListItemType type, string colname,int j)
    {
    templateType = type;
    columnName = colname;
    i = j;
    } public void InstantiateIn(System.Web.UI.Control container)
    {

    switch(templateType)
    {
    case ListItemType.Header:
    Label lb = new Label();
    lb.Text=GetHeadText(columnName,i);
    lb.ForeColor = Color.White;
    container.Controls.Add(lb);
    break;
    case ListItemType.Item:
    System.Web.UI.WebControls.CheckBox cb = new CheckBox();
    //cb.ID = "cb"+i.ToString();
    cb.DataBinding += new EventHandler(cb_DataBinding);
    container.Controls.Add(cb);
    break; }
    }
    //得到head标题
    public string GetHeadText(string partname ,int i)
    {
    string[] part = partname.Split('-');
    return part[i].ToString();
    } //得到checkbox的值        
    public void cb_DataBinding(object sender,System.EventArgs e)
    {
    System.Web.UI.WebControls.CheckBox cb;
    cb =(CheckBox) sender;
    DataGridItem  container = (DataGridItem)cb.NamingContainer ;
    cb.Checked = GetCheck(((DataRowView)container.DataItem)[columnName].ToString().Substring(i,1)); } //把1转化为true,0转化为false
    public bool GetCheck(string r)
    {
    if(r=="0")
    {
    return false;
    }
    else
    {
    return true;
    }
    }
    }

    } // public void lb_DataBinding(object sender,System.EventArgs e)
    // {
    // LinkButton lb;
    // lb =(LinkButton) sender;
    // DataGridItem  container = (DataGridItem)lb.NamingContainer ;
    // DepartmentName dp = new DepartmentName();
    // string[] dpName = dp.DepartName().Split('-');
    // lb.Text = dpName[i] ;
    //
    //
    //
    // }
      

  7.   

    可参考
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchtopquestionsaboutaspnetdatagridservercontrol.asp如果非不要,不建议动态添加
      

  8.   

    应该把BindGrid(ddl_department.SelectedItem.Value);
    移到if(!IsPostBack)的外面,
    if(!IsPostBack)
    {  
    BindDepartment();}
    BindGrid(ddl_department.SelectedItem.Value);
      

  9.   

    lb_save_Click里的BindGrid(ddl_department.SelectedItem.Value);注掉试试