模板的动态生成,和数据绑定到是可以实现
可当用户更改其中一个TExtbox中的值的时候怎么才能把它保存起来,以便更新或是增加记录所用呢
//
程序流程:private void Page_load(....)
{
  AddTemplate()   //增加模板列
  {}
  .
  .
  .
  CreateTempTable() //本地临时表
}
btn_updateClick()//更新事件
{
 //取不到用户的更改的值
}
当用户一单击Btn_updateClick时,就进入了Page_load()事件,它又从数据库里取值了,也就是说,用户的更改没有起到做用//
望 高 手 指 教 !

解决方案 »

  1.   

    在page_load()里面判断IsPostBackif (!IsPostBack)
    {
    AddTemplate()   //增加模板列
      {}
      .
      .
      .
      CreateTempTable() //本地临时表}
      

  2.   

    michleliu(浪子听天) 
    照你的说法,那我一单击那个更新按钮,就什么也没有了
    因为,你没有设计给DataGrid任何列,也没有做数据绑定
      

  3.   

    先确定你的网页是否设置了EnableViewState,应该为true,即启用;
    在datagrid中使用html控件,这样可以保持你给他设定的name属性和id属性,
    如果使用服务端的web控件,展现的时候这些属性可能会被修改;
    使用isPostBack属性,即“michleliu(浪子听天) ”所说的那样:
    if (!IsPostBack)
    {
      AddTemplate()   //增加模板列
    }
    else
    {
      //处理修改
    }
      

  4.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack && Session["usercode"]!=null)
    {
    scstr=Application["scstr"].ToString();
    SortedList period=(SortedList)Session["period"];//调查周期信息
    SortedList sp=(SortedList)Session["sp"]; //调查专业信息
    SortedList indicate=(SortedList)Session["ind"]; //调查的指标信息
    //
    tempTable=Create_Temp_Table();
    //设置表格的头信息
    AddColumns();
    //
    DataGrid2.Caption="您选择的专业信息:"+sp.GetByIndex(0).ToString()+"   调查周期:"+period.GetByIndex(0).ToString()+"   调查指标:"+indicate.GetByIndex(0).ToString();
    //
    DataGrid2.DataSource=tempTable;
    DataGrid2.DataBind();
    }
    }private void AddColumns()
    {
    SortedList cols=(SortedList)Session["cols"];

    //*****对表格增加模板列******
    for(int colsIndex=0;colsIndex<=tempTable.Columns.Count-1;colsIndex++)
    {
    if(colsIndex==0) //**表格的第一列不要求可编辑
    {
    TemplateColumn tc1=new TemplateColumn();
    //
    tc1.ItemStyle.Width=new Unit(120);
    tc1.HeaderTemplate=new DataGridTemplate(ListItemType.Header,tempTable.Columns[0].ColumnName,cols.GetKey(colsIndex).ToString());
    tc1.ItemTemplate=new DataGridTemplate(ListItemType.Item,tempTable.Columns[0].ColumnName,cols.GetKey(colsIndex).ToString());
    //
    DataGrid2.Columns.Add(tc1);
    }
    else
    {
    TemplateColumn tc=new TemplateColumn();
    //
    tc.HeaderTemplate=new DataGridTemplate(ListItemType.Header,tempTable.Columns[colsIndex].ColumnName,tempTable.Columns[colsIndex].ColumnName);
    tc.ItemTemplate=new DataGridTemplate(ListItemType.EditItem,tempTable.Columns[colsIndex].ColumnName,tempTable.Columns[colsIndex].ColumnName);
    //
    DataGrid2.Columns.Add(tc);

    }
    }
    ButtonColumn bc=new ButtonColumn();
    //
    bc.ButtonType=ButtonColumnType.PushButton;

    bc.CommandName="SaveData";
    bc.HeaderText="操作";
    bc.Text="保存数据";
    //
    DataGrid2.Columns.Add(bc);
    }//*****更新按钮******
    foreach(DataGridItem dgi in DataGrid2.Items)
    {
    //获取地区代码
    string areacode=rows.GetKey(dgi.ItemIndex).ToString();

    //TODO:获取年份并设置sql脚本(列数据)
    for(int tmpVar=0;tmpVar<=cols.Count-1;tmpVar++)
    {
    int ff=DataGrid2.Items.Count;
    string itemType=dgi.ItemType.ToString();
    string id=cols.GetKey(tmpVar).ToString();
    TextBox tmpbox=(TextBox)dgi.FindControl(cols.GetKey(tmpVar).ToString()); string text=tmpbox.Text; int dataValue=Convert.ToInt32(text);
    //行数累计
    string str="select * from base_data where areacode='"+areacode+"' and datecode='"+cols.GetKey(tmpVar).ToString()+"' and indcode='"+indicate.GetKey(0).ToString()+"' and spcode='"+sp.GetKey(0).ToString()+"' and periodcode='"+period.GetKey(0).ToString()+"'";
    //
    int rowsAddUp=0;
    rowsAddUp=SqlHelper.ExecuteDataset(scstr,CommandType.Text,str).Tables[0].Rows.Count;
    //
    if(rowsAddUp==0)//增加记录
    {
    string insert_str="insert into base_data values('"+areacode+"','"+cols.GetKey(tmpVar).ToString()+"','"+indicate.GetKey(0).ToString()+"',"+dataValue+",'"+period.GetKey(0).ToString()+"','','"+sp.GetKey(0).ToString()+"','')";
    SqlHelper.ExecuteNonQuery(scstr,CommandType.Text,insert_str);
    }
    else //更新记录
    {
    string update_str="update base_data set datavalue="+dataValue+" where areacode='"+areacode+"' and datecode='"+cols.GetKey(tmpVar).ToString()+"' and indcode='"+indicate.GetKey(0).ToString()+"' and spcode='"+sp.GetKey(0).ToString()+"' and periodcode='"+period.GetKey(0).ToString()+"'";
    SqlHelper.ExecuteNonQuery(scstr,CommandType.Text,update_str);
    }
    }
    }
    }
      

  5.   

    查看示例程序:
    ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemWebUIWebControlsDataGridClassUpdateCommandTopic.htm在“开始——运行”直接到开
      

  6.   

    DateGrid之外隐藏一个TextBox来接收TExtbox的值,update 这个TextBox的Text就可以了!