无法更新.net中的gridview值

时间:2015-12-14 12:00:57

标签: c# asp.net gridview

更新gridview记录时。旧值只会更新。我正在使用绑定字段。在im中获取变量中的数据时获取旧值。

<asp:GridView runat="server" ID="GvLeads" AutoGenerateColumns="false" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true" OnRowDeleting="GvLeads_RowDeleting" OnRowEditing="GvLeads_RowEditing" OnRowCancelingEdit="GvLeads_RowCancelingEdit" EmptyDataText="No Records Found" OnRowUpdating="GvLeads_RowUpdating" OnRowDeleted="GvLeads_RowDeleted">
 <Columns>
    <asp:BoundField HeaderText="Id" DataField="LeadId" />
    <asp:BoundField HeaderText="Company" DataField="Companyname" />
 </Columns>
</GridView >

背后的代码

protected void GvLeads_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

    GridViewRow row = GvLeads.Rows[e.RowIndex];
    String str = ((TextBox)(row.Cells[1].Controls[0])).Text;
    int Leadid = Convert.ToInt32(str);
    string CompanyName = ((TextBox)(row.Cells[2].Controls[0])).Text;
 }

1 个答案:

答案 0 :(得分:1)

当您在Page_Load事件被调用之前调用RowUpdating事件时,通常会在Page_Load填充网格时发生这种情况,该事件将使用初始值填充网格。如何避免?为此目的使用!IsPostBack

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGrid(); // For e.g.
    }
}