_client._ClientName = ((TextBox) e.Item.Cells[1].Controls[2]).Text.Trim();如象这个语句,只能获取原来邦定在该控件上的值,而不能获取更改后的值,怎么办?5555555555。3个小时了。。我都没查出来啊,高手帮帮忙阿。

解决方案 »

  1.   

    用GridView吧,编辑更新都写好了,直接用就是了
      

  2.   

    语句没有错。你是写在Update_Command事件中吗?
      

  3.   

    这个写在Update_Command事件中吗
    是的
      

  4.   

    是不是要加条 .uptdata 语句?
      

  5.   

    在Page_Load()
    事件中加上
    if(!IsPostBack)
    {
        
    }
    估计就ok了
      

  6.   

    uptdata是啥意思?不是太明白
      

  7.   

    zhulei2008(滴水浪流) i try...
      

  8.   

    datagrid1中,如果使用模版的话,选择datagrid1的updateCommand,然后设立主键
    DataGrid1.DataKeys(e.Item.ItemIndex)
    在datagrid1.datakeyField设置关键字段
    然后申明引用一个textbox控件
    dim textBoxName as textbox
    然后找到你datagrid1中的这个控件
    textBoxName = e.Item.FindControl("textbox2")
    这样就找到控件了
    然后用sqlCommand对象,就可以更新了
    更新他用textBoxName.text.trim---这个就是他的值了我也是初学,应该就是这样
      

  9.   

    alasunny(阿拉) 我知道你的意思啊,但是我也是这样写的阿,可是我获取不了用户更改后的值啊
      

  10.   

    你是不是在page_load的时候从数据库里取值绑定在控件上,然后点按钮引发事件,把页面上控件内改变过的值更新到数据库?如果是这样的话,点击按钮产生回调,画面生成的时候还是会从数据库里取值绑定到控件,把你填写的内容覆盖了,所以从数据库里取默认值绑定到控件要写在
    if (!Page.PostBack){从数据库取数据绑定到控件} 里.
      

  11.   

    ????用户更改后的值~
    在asp.net中,页面打开,你点编辑,此时就发生了很多事件了,这个时候,我们得到了现在datagrid里面的控件值了,你不产生提交事件,如何获得人家输入的值??
      

  12.   

    是不是你的页面首次加载
    if(!IsPostBack)
    {
        绑定数据;
    }
    在更新完毕后还要重新绑定数据!
      

  13.   

    在更改后一定要记得写入 this.BindingContext[dataSet, "tableName"].EndCurrentEdit();
      

  14.   

    我的代码:
    private void Page_Load(object sender, System.EventArgs e)
    {
    BindGrid();
    }private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {

    _client._ClientID=Convert.ToInt32(((Label)(e.Item.Cells[0].Controls[1])).Text);
      Message(((Label)(e.Item.Cells[0].Controls[1])).Text);
    _client._ClientLeading = ((CheckBox) (e.Item .Cells[1].Controls[1])).Checked ? 1 : 0;

    //  Message(e.Item.Cells[4].Text);
    //  Message(e.Item.Cells[5].Text);
    //  Message(e.Item.Cells[6].Text);
    //_client._ClientName = ((TextBox) e.Item.Cells[1].Controls[2]).Text.Trim();
    // TextBox t;
    // t = (TextBox) e.Item.Cells[4].Controls[1];
    // string str = t.Text;
    // Message(str);

    //Message(((TextBox) e.Item.Cells[1].FindControl("TextBox1")).Text.Trim());
    _client._ClientDepartment = ((TextBox) e.Item.Cells[2].Controls[1]).Text;
    //Message(((TextBox) e.Item.Cells[2].Controls[1]).Text);
    _client._ClientEmail = ((TextBox) e.Item.Cells[3].Controls[1]).Text;
    //Message(((TextBox) e.Item.Cells[3].Controls[1]).Text);
    _client._ClientTelphone = ((TextBox) e.Item.Cells[4].Controls[1]).Text;
    //Message(((TextBox) e.Item.Cells[4].Controls[1]).Text);
    _client._ClientLevel= ((DropDownList) e.Item.Cells[5].Controls[1]).DataTextField.Trim();
    //Message(((DropDownList) e.Item.Cells[5].Controls[1]).SelectedItem.Text.Trim());
    _client._State= ((DropDownList) e.Item.Cells[6].Controls[1]).DataTextField.Trim();
    //Message(((DropDownList) e.Item.Cells[6].Controls[1]).SelectedItem.Text.Trim());
        _client._SellerName= ((TextBox) e.Item.Cells[7].Controls[1]).Text;
    //Message(((TextBox) e.Item.Cells[7].Controls[1]).Text);
    ChangeClient _changeclient = new ChangeClient();
    if(_changeclient.Update(_client))
    {
    Message("更新成功");
    }
    else
    {
    Message("更新失败");
    }
    this.DataGrid1.EditItemIndex = -1;
    TreeToBindGrid();


     
    }
    #endregion
      

  15.   

    Response.Write(TransformXml(ds, Server.MapPath("XSLTFile.xsl")));        DataRow dr = dt.NewRow();
            dr["test"] = "";
            dt.Rows.Add(dr);
      

  16.   

    上边那个太乱了:private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {

    _client._ClientID=Convert.ToInt32(((Label)(e.Item.Cells[0].Controls[1])).Text);
    Message(((Label)(e.Item.Cells[0].Controls[1])).Text);
    _client._ClientLeading = ((CheckBox) (e.Item .Cells[1].Controls[1])).Checked ? 1 : 0;

    //Message(e.Item.Cells[4].Text);
    //Message(e.Item.Cells[5].Text);
    //Message(e.Item.Cells[6].Text);
    //_client._ClientName = ((TextBox) e.Item.Cells[1].Controls[2]).Text.Trim();
    //TextBox t;
    //t = (TextBox) e.Item.Cells[4].Controls[1];
    //string str = t.Text;
    //Message(str);

    //Message(((TextBox) e.Item.Cells[1].FindControl("TextBox1")).Text.Trim());
    _client._ClientDepartment = ((TextBox) e.Item.Cells[2].Controls[1]).Text;
    //Message(((TextBox) e.Item.Cells[2].Controls[1]).Text);
    _client._ClientEmail = ((TextBox) e.Item.Cells[3].Controls[1]).Text;
    //Message(((TextBox) e.Item.Cells[3].Controls[1]).Text);
    _client._ClientTelphone = ((TextBox) e.Item.Cells[4].Controls[1]).Text;
    //Message(((TextBox) e.Item.Cells[4].Controls[1]).Text);
    _client._ClientLevel= ((DropDownList) e.Item.Cells[5].Controls[1]).DataTextField.Trim();
    //Message(((DropDownList) e.Item.Cells[5].Controls[1]).SelectedItem.Text.Trim());
    _client._State= ((DropDownList) e.Item.Cells[6].Controls[1]).DataTextField.Trim();
    //Message(((DropDownList) e.Item.Cells[6].Controls[1]).SelectedItem.Text.Trim());
    _client._SellerName= ((TextBox) e.Item.Cells[7].Controls[1]).Text;
    //Message(((TextBox) e.Item.Cells[7].Controls[1]).Text);
    ChangeClient _changeclient = new ChangeClient();
    if(_changeclient.Update(_client))
    {
    Message("更新成功");
    }
    else
    {
    Message("更新失败");
    }
    this.DataGrid1.EditItemIndex = -1;
    TreeToBindGrid();


    }
    #endregion
      

  17.   

    ~~~~~~~~~~~~~~~c#的?
    糟糕了,看不懂,我只能看看vb的
      

  18.   

    大家帮我看看吧,我是点击更新的时候已经帮TextBox里的值已经改变了阿。但是每一个Message的消息都还是原来我帮定在上边的数据。。
      

  19.   

    恩在页面吗?我的那个页面就像CSDN那样阿,在左边有一棵树。。要不断地刷新阿,要了if(!IsPostBack)就不能刷新了阿
      

  20.   

    在Page_Load()加上
    private void Page_Load(object sender, System.EventArgs e)
    {
    if(!IsPostBack)
    {
        BindGrid();
    }}
      

  21.   

    private void Page_Load(object sender, System.EventArgs e)
    {
      if(!IsPostBack)
         BindGrid();
    }
    这种问题你调试下就知道怎么办了阿;呵呵
    看来楼主不明白 IsPostBack 的用处
      

  22.   

    _client._ClientID=Convert.ToInt32(((Label)(e.Item.Cells[0].Controls[1])).Text);改成
    _client._ClientID=Convert.ToInt32(((Label)(e.Item.Cells[0].Controls[0])).Text);
    试试啊 ,全部改成Controls[0]
      

  23.   

    试试啊 ,全部改成Controls[0]
    ----------
    改这个有什么用哩 第3列当然就是Controls[2]
    获取不了用户更改后的值
    ----------
    这个问题本周我也碰到了!!!-_-原来写的时候能读到的 本周重写就不行了 或许是有些小细节没注意到吧
      

  24.   

    sbqcel(灯枯油尽) ( ) 写进去了,还不行了,怎么办
      

  25.   

    更新后 DATABIND() 了吗...
      

  26.   

    if(!IsPostBack)
    {
        绑定DataGrid
    }
      

  27.   

    全部改成Controls[0]
    错误啊
      

  28.   

    同意用ISPOSTBACK解决。你修改的数据每次都被页面重新邦定给冲掉了。
      

  29.   

    一定要
    if(!IsPostBack)
    {
        
    }
      

  30.   

    跟Controls[0],Controls[1]没关系
      

  31.   

    if(!IsPostBack)
    {
        绑定DataGrid
    }肯定是触发更新时POSTBACK了,然后又把数据RELOAD一遍了.其它的数据能更新吗???如果不能的话,那么就试一下这个吧.放在PAGE.LOAD事件下面.
      

  32.   

    if(!IsPostBack)
    {
        
    }绝对是这个问题。。我肯定。。
      

  33.   

    Controls[1],这个是控件你里面只有这个控件,当然要改拉,前面是cells[]才是单元格里面的文本,不懂就不要说了,回去查查苯
      

  34.   

    呵呵,我搞定了阿,谢谢大家了,原来是。。我少写了一个东西原来的DataBind()是写在IsPostBack里的阿,但是我还有一个绑定没有写在里边阿,这个绑定每次也要向服务器发送一次请求阿,大意阿,。。谢谢大家了,希望大家以后一定不要犯我这样的错误啊,祝大家工资越拿越多。
      

  35.   

    解决方法:  用SqlCommandBuilder 实现批量更新  1.功能:    可以实现你对DataSet在UI层做任意操作后,直接丢给这个方法,这个方法就可以自动把你的修改更 新到数 据库中,而没必要每次都更新到   数据库  2.使用方法
      public DataSetUpdateByDataSet(DataSet ds,string strTblName,string strConnection)
      {
          SqlConnection  conn = new SqlConnection(strConnection));
             
         SqlDataAdapter myAdapter = new SqlDataAdapter();
         SqlCommand myCommand = new SqlCommand("select * from "+strTblName),(SqlConnection) conn);    
         myAdapter.SelectCommand = myCommand;
         SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);     
      try      {                 lock(this)            //处理并发情况(分布式情况)              {               myAdapter.Update(ds,strTblName);                }      }  
     
      catch(Exception err)
     {   conn.Close();   
       throw new BusinessException(err);
     }    return ds;    //数据集的行状态在更新后会都变为: UnChange,在这次更新后客户端要用返回的ds}或public DataSet UpdateByDataSet(DataSet ds,string strTblName,string strConnection)
      {
       
      
          SqlConnection  conn = new SqlConnection(strConnection));         
          SqlCommand myCommand = new SqlCommand("select * from "+strTblName),(SqlConnection) conn);          SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand );         SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);     
          
          myAdapter.InsertCommand = myCommandBuilder .GetInsertCommand();      myAdapter.UpdateCommand = myCommandBuilder .GetUpdateCommand();     myAdapter.DeleteCommand = myCommandBuilder .GetDeleteCommand();   try  {        lock(this)                              //处理并发情况(分布式情况)       {              conn.Open();              myAdapter.Update(ds,strTblName);                conn.Close();       }
      
             return ds;    //数据集的行状态在更新后会都变为: UnChange,在这次更新后客户端要用返回的ds }
     catch(Exception err)
     {       conn.Close(); 
          throw new BusinessException(err);
     }
    }直接调用这两个方法的任意一个就OK啦,说明的一点是select * from "+strTblName是一定要的,
    作用大家也应该想到了,主要是告诉 SqlDataAdapter更新哪个表
      

  36.   

    在asp.net中,如何一次性更新datagrid中的所有记录呢?可以用如下的方法,首先,
    要对DATAGRID中要更新的列建立模版列,比如:
    asp:datagrid id="dgPopularFAQs" runat="server"
    AutoGenerateColumns="False"
    ...>  <Columns>
        <asp:BoundColumn DataField="FAQID" ItemStyle-Width="10%"
    ItemStyle-HorizontalAlign="Center" HeaderText="FAQ ID" />    <asp:BoundColumn DataField="CategoryName" HeaderText="Category" />
        
        <asp:TemplateColumn HeaderText="Question">
          <ItemTemplate>
            <asp:TextBox runat="server" id="txtDescription" Columns="75" 
                 Text='<%# Container.DataItem("Description") %>' />
          </ItemTemplate>
        </asp:TemplateColumn>
        
        <asp:TemplateColumn HeaderText="Submitted By">
          <ItemTemplate>
            <asp:TextBox runat="server" id="txtSubmittedBy"
                 Text='<%# Container.DataItem("SubmittedByName") %>' />
          </ItemTemplate>
        </asp:TemplateColumn>  </Columns> 
    </asp:datagrid>
    之后,对DATAGRID进行一次遍历,
    Dim myConnection as New SqlConnection(connection string)
    Dim myCommand as New SqlCommand(strSQL, myConnection)Dim dgi as DataGridItem
    For Each dgi in dgPopularFAQs.Items
      'Read in the Primary Key Field
      Dim id as Integer = Convert.ToInt32(dgPopularFAQs.DataKeys(dgi.ItemIndex))
      Dim question as String = CType(dgi.FindControl("txtDescription"), TextBox).Text
      Dim submittedBy as String = CType(dgi.FindControl("txtSubmittedBy"), TextBox).Text
          
      'Issue an UPDATE statement...
      Dim updateSQL as String = "UPDATE TableName SET Question = @Question, " & _
                  "SubmittedByName = @SubmittedByName WHERE FAQID = @ID"
      myCommand.Parameters.Clear()
      myCommand.Parameters.Add("@Question", question)
      myCommand.Parameters.Add("@SubmittedByName", submittedBy)
      
      myCommand.ExecuteNonQuery()
    Next
      

  37.   

    DataGrid 的绑定是怎么设置的 
    我的情况是
    第一次 把ID字段设成了不显示,然后e.Item.Cells[0]里面就找不到任何的Controls
    第二次 把ID字段设成了显示、只读,然后e.Item.Cells[0].Text能获得ID