无法更新.net中的网格视图值

本文关键字:网格 视图 更新 net | 更新日期: 2023-09-27 18:26:46

更新网格视图记录时。旧值只更新。我正在使用绑定字段。在获取变量中的数据时获取旧值。

<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;
 }

无法更新.net中的网格视图值

当您在Page_Load填充网格时,在调用Page_Load事件之前,一旦调用了RowUpdating事件,就会发生这种情况,该事件会用初始值填充网格。如何避免?为此使用!IsPostBack

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