//前台代码
<asp:GridView ID="GView" runat="server" AutoGenerateColumns="False" CellPadding="4"
                        OnRowDeleting="GView_RowDeleting" OnRowEditing="GView_RowEditing"
                        OnRowUpdating="GView_RowUpdating" 
            OnRowCancelingEdit="GView_RowCancelingEdit" DataKeyNames="UserName" 
            GridLines="None">
                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                        <Columns>
                            <asp:BoundField DataField="UserName" HeaderText="用户名" ReadOnly="True" />
                            <asp:TemplateField HeaderText="权限组">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("GroupID") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("GroupID") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="密码">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("UserPassword") %>'></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("UserPassword") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                            <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
                       </Columns>
        </asp:GridView>
//后台代码
    protected void GView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GView.EditIndex = e.NewEditIndex;
        GViewBind();
    }
    protected void GView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GView.EditIndex = -1;
        GViewBind();
    }
    protected void GView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox p = GView.Rows[e.RowIndex].FindControl("TextBox2") as TextBox;
        TextBox g = GView.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
        string pwd1 = md5(pwd, 16);
        int State;
            DataAccess lj = new DataAccess();
            SqlConnection conn = new SqlConnection(lj.SqlBase);
            conn.Open();
            Response.Write("<script>alert('" + p.Text + "');</script>");//得到的还是原来地值?
            Response.Write("<script>alert('" + g.Text + "');</script>");//得到的还是原来地值?
        State = lj.ModInfo("update PE_User set GroupID='"
            + groupid + "',UserPassword='"
            + pwd1 + "' where UserName='" + GView.Rows[e.RowIndex].Cells[0].Text.ToString() + "'", conn);  
        if (State == 1)
        {
            GView.EditIndex = -1;
            GViewBind();
        }
        else
        {
            GView.EditIndex = -1;
            Response.Write("<script>alert('修改失败,请联系数据库管理员!');</script>");
        }
    }

解决方案 »

  1.   

    参考:http://blog.csdn.net/insus/archive/2008/03/30/2229898.aspx
      

  2.   

       我把后台地 TextBox p = GView.Rows[e.RowIndex].FindControl("TextBox2") as TextBox; 
            TextBox g = GView.Rows[e.RowIndex].FindControl("TextBox1") as TextBox; 
    改为     
            string id =GView.DataKeys[e.RowIndex].Value.ToString();
            GridViewRow gvr = GView.Rows[e.RowIndex];
            string strtxtQuan=((TextBox)gvr.FindControl("TextBox1")).Text;
            string strtxtMi = ((TextBox)gvr.FindControl("TextBox2")).Text;
    还是不行啊
    原来地数据是 123456789 我改成987  用Response.Write("<script>alert(' + strtxtmi + ');</script>");还是显示 123456789
      

  3.   

    搞定了,原来  page_load 里面有 gvbind() 加个 if(!ispostback){gvbind(); }