我用 dataGrid绑订了数据库的字段,点击编辑后出现文本框,我更改后击发了updata事件,但取文本框中的值还是原来的值
例如
TextBox t = (TextBox)e.item.findctrol("tt");或
TextBox t = (TextBox)e.cells(1).ctrols(0);
string temp =t.text;
temp 还是原来的值。
以上两种方法我都试了,都是取得原来数据库的值。
怎样才能取得你修改后的值呢(既当前值)?我用的是c#(asp.net)写的!

解决方案 »

  1.   

    你没有把拌定数据库的代码写在page_load事件中的
    if(!IsPostBack)
    {
    ///这里写你拌定数据库的代码
    }
      

  2.   

    我按照以上的写法试了,还是取不到更新后的数据啊!以下是我的程序
    界面:
    <form id="DataGrade" method="post" runat="server">
    <asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 205px; POSITION: absolute; TOP: 136px" runat="server" AutoGenerateColumns="False" Width="219px" OnSortCommand="SortRows" AllowSorting="True" AllowPaging="True" OnPageIndexChanged="ChangeGridPage" Height="307px" OnEditCommand="DoItemEdit" OnUpdateCommand="DoItemUpdate" OnCancelCommand="DoItemCancel" DataKeyField="emp_id">
    <SelectedItemStyle Font-Size="X-Small"></SelectedItemStyle>
    <AlternatingItemStyle Font-Size="X-Small"></AlternatingItemStyle>
    <ItemStyle Font-Size="X-Small"></ItemStyle>
    <HeaderStyle Font-Size="X-Small"></HeaderStyle>
    <Columns>
    <asp:BoundColumn DataField="emp_id" ReadOnly="True" HeaderText="emp_id"></asp:BoundColumn>
    <asp:BoundColumn DataField="fname" HeaderText="fname"></asp:BoundColumn>
    <asp:TemplateColumn HeaderText="lname">
    <ItemStyle Font-Size="X-Small"></ItemStyle>
    <ItemTemplate>
    <asp:Label text='<%# DataBinder.Eval(Container.DataItem,"lname")  %>' Runat="server"/>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtLname" Width="60" Text='<%# DataBinder.Eval(Container.DataItem,"lname") %>' Runat="server"/>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="&ETH;&THORN;&cedil;&Auml;/±&pound;&acute;&aelig;" CancelText="&Egrave;&iexcl;&Iuml;&ucirc;" EditText="±à&frac14;&shy;"></asp:EditCommandColumn>
    </Columns>
    </asp:datagrid><asp:textbox id="TextBox1" style="Z-INDEX: 102; LEFT: 207px; POSITION: absolute; TOP: 96px" runat="server" Width="102px"></asp:textbox></form>
    实现
    public class DataGrade : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    public string strsort="";
    private void Page_Load(object sender, System.EventArgs e)
    {
    // &Ocirc;&Uacute;&acute;&Euml;&acute;&brvbar;·&Aring;&Ouml;&Atilde;&Oacute;&Atilde;&raquo;§&acute;ú&Acirc;&euml;&Ograve;&Ocirc;&sup3;&otilde;&Ecirc;&frac14;&raquo;&macr;&Ograve;&sup3;&Atilde;&aelig;

    if(!Page.IsPostBack)
    {


    BindDataGrid();
    }

    }
    public void BindDataGrid()
    {
    string sqlConn = "server=localhost;uid=sa;pwd=fishbaby;database=yuyc";
    string sqlSelect = "select * from employee";
    SqlConnection conn = new SqlConnection(sqlConn);
    try
    {

    conn.Open();
    //SqlCommand comm = new SqlCommand(sqlSelect,conn);
    SqlDataAdapter adapter = new SqlDataAdapter(sqlSelect,conn);
    DataSet dataSet = new DataSet();
    adapter.Fill(dataSet,"employee");
    //SqlDataReader dataReader = comm.ExecuteReader();
    DataView dataView = new DataView(dataSet.Tables["employee"]);
    //dataView.Sort=TextBox1.Text;
    DataGrid1.PageSize=4;
    DataGrid1.DataSource=dataView;
    DataGrid1.DataBind();
    }
    catch(Exception ex)
    {
    ex.ToString();
    }
    finally
    {
    if(conn != null)
    { //
    conn.Close();
    //conn = null; }    }

    }
    public void DoItemUpdate(object sender,System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if(Page.IsValid)
    {
    TextBox txtFname = new TextBox();
    TextBox txtLname = new TextBox();
    txtLname= (TextBox)e.Item.FindControl("txtLname");
    //txtFname = (TextBox)e.Item.Cells[2].Controls[0];
    string strTxt = txtLname.Text;
    string strFname = txtFname.Text;
    string strsql = "update employee set lname='"+txtLname.Text+"' where emp_id='"+ DataGrid1.DataKeys[e.Item.ItemIndex] +"'";
    string sqlConn = "server=localhost;uid=sa;pwd=fishbaby;database=yuyc";
    SqlConnection conn = new SqlConnection(sqlConn);
    conn.Open();
    SqlCommand comm = new SqlCommand(strsql,conn);
    int i = comm.ExecuteNonQuery();
    if(i>0)
    {
    DataGrid1.EditItemIndex = -1;
    BindDataGrid();
    }
    else
    {
    TextBox1.Text = "&cedil;ü&ETH;&Acirc;&Ecirc;§°&Uuml;&pound;&iexcl;";
    }

    }

    //SqlDataReader dataReader = comm.ExecuteReader();
    }
    public void DoItemCancel(object sender,System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    DataGrid1.EditItemIndex = -1;
    BindDataGrid();
    }
    public void DoItemEdit(object sender,System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    DataGrid1.EditItemIndex =(int)e.Item.ItemIndex;
    BindDataGrid();
    }
    public void ChangeGridPage(object sender,System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    DataGrid1.CurrentPageIndex = e.NewPageIndex;
    BindDataGrid();
    }
    public void SortRows(object sender,System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
    DataGrid1.EditItemIndex = -1;
    strsort = e.SortExpression.ToString();
    BindDataGrid();
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN&pound;&ordm;&cedil;&Atilde;&micro;÷&Oacute;&Atilde;&Ecirc;&Ccedil; ASP.NET Web &acute;°&Igrave;&aring;&Eacute;è&frac14;&AElig;&AElig;÷&Euml;ù±&Oslash;&ETH;è&micro;&Auml;&iexcl;&pound;
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// &Eacute;è&frac14;&AElig;&AElig;÷&Ouml;§&sup3;&Ouml;&Euml;ù&ETH;è&micro;&Auml;·&frac12;·¨ - &sup2;&raquo;&Ograve;&ordf;&Ecirc;&sup1;&Oacute;&Atilde;&acute;ú&Acirc;&euml;±à&frac14;&shy;&AElig;÷&ETH;&THORN;&cedil;&Auml;
    /// &acute;&Euml;·&frac12;·¨&micro;&Auml;&Auml;&Uacute;&Egrave;&Yacute;&iexcl;&pound;
    /// </summary>
    private void InitializeComponent()
    {    
    this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.ChangeGridPage);
    this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DoItemCancel);
    this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DoItemEdit);
    this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.SortRows);
    this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DoItemUpdate);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    }
    请大家帮我看看啊!