Asp.net GridView';在OnRowUpdate事件中,s NewValues和OldValues为空

Asp.net GridView';在OnRowUpdate事件中,s NewValues和OldValues为空,asp.net,gridview,asp.net-3.5,Asp.net,Gridview,Asp.net 3.5,我有下面的网格视图。我绑定到代码隐藏中的自定义数据源。它很好地进入了“onrowUpdate”事件,但是没有新值或旧值。关于如何获得这些值,有什么建议吗 <asp:GridView ID="gv_Personnel" runat="server" OnRowDataBound="gv_Personnel_DataBind" OnRowCa

我有下面的网格视图。我绑定到代码隐藏中的自定义数据源。它很好地进入了“onrowUpdate”事件,但是没有新值或旧值。关于如何获得这些值,有什么建议吗

<asp:GridView   ID="gv_Personnel" 
                        runat="server" 
                        OnRowDataBound="gv_Personnel_DataBind" 
                        OnRowCancelingEdit="gv_Personnel_CancelEdit" 
                        OnRowEditing="gv_Personnel_EditRow" 
                        OnRowUpdating="gv_Personnel_UpdateRow"
                        AutoGenerateColumns="false" 
                        ShowFooter="true" 
                        DataKeyNames="BudgetLineID"
                        AutoGenerateEditButton="true" 
                        AutoGenerateDeleteButton="true"
                        >
            <Columns>                 
                <asp:BoundField HeaderText="Level of Staff" DataField="LineDescription" />
                <%--<asp:BoundField HeaderText="Hrs/Units requested" DataField="NumberOfUnits" />--%>
                <asp:TemplateField HeaderText="Hrs/Units requested">
                    <ItemTemplate>
                        <%# Eval("NumberOfUnits")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="tb_NumUnits" runat="server" Text='<%# Bind("NumberOfUnits")%>' />
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="Hrs/Units of Applicant Cost Share" DataField="" NullDisplayText="0" />
                <asp:BoundField HeaderText="Hrs/Units of Partner Cost Share" DataField="" NullDisplayText="0" />
                <asp:BoundField FooterStyle-Font-Bold="true" FooterText="TOTAL PERSONNEL SERVICES:" HeaderText="Rate" DataFormatString="{0:C}" DataField="UnitPrice" />
                <asp:TemplateField HeaderText="Amount Requested" ItemStyle-HorizontalAlign="Right" FooterStyle-HorizontalAlign="Right"  FooterStyle-BorderWidth="2" FooterStyle-Font-Bold="true"/>
                <asp:TemplateField HeaderText="Applicant Cost Share" ItemStyle-HorizontalAlign="Right" FooterStyle-HorizontalAlign="Right" FooterStyle-BorderWidth="2" FooterStyle-Font-Bold="true"/>
                <asp:TemplateField HeaderText="Partner Cost Share" ItemStyle-HorizontalAlign="Right" FooterStyle-HorizontalAlign="Right" FooterStyle-BorderWidth="2" FooterStyle-Font-Bold="true"/>
                <asp:TemplateField HeaderText="Total Projet Cost" ItemStyle-HorizontalAlign="Right" FooterStyle-HorizontalAlign="Right" FooterStyle-BorderWidth="2" FooterStyle-Font-Bold="true"/>
            </Columns>
        </asp:GridView>

我不确定这是否有用……但这是我在msdn中找到的

只有当GridView控件通过使用DataSourceID属性绑定到数据时,才会自动填充Keys、OldValues和NewValues集合

关于GridView控件的 行更新事件问题,它是 预期的行为,因为当我们 不关联GridView(或其他 ASP.NET 2.0数据绑定控件)和 数据源控件,它不会 自动查询并填写 的参数集合 更新/删除/。。。事件。这样 在这种情况下,我们需要手动提取 模板中的字段值 控制

这是一位微软员工在办公室里说的话

在这种情况下,您可以使用方法自己创建NewValues集合

编辑:

我在博客的评论中发现了一段代码:

protectedvoid OnRowEditing(对象发送方,GridViewEditEventArgs e)
{     
GridView gv=(GridView)发送方;
gv.EditIndex=e.NewEditIndex;
gv.DataBind();
...
}
受保护的void onrowUpdate(对象发送方,GridViewUpdateEventArgs e)
{
GridView gv=(GridView)发送方;
对于(int i=0;i

尚未测试,但似乎解决了问题,请告诉我是否有。

我正在我的页面加载事件中进行数据绑定,当我单击“更新”时,回发完成,页面加载事件也启动了。GridView再次被旧值填充,所以在myGrid.RowUpdate事件中,我无法检索新值。如果这有帮助

您需要在表单加载中检查iPostBack。当gridview的更新执行回发时,它首先使用旧值刷新。使用IsPostback可防止出现以下情况:

If Not IsPostback Then 

   ' Data bind here

End If

这样,它将不会覆盖页面PreRender事件处理程序中的“新”gridview值,如下所示:

Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
    GridBinder()
End Sub

注意:预渲染发生在RowUpdate事件之后。

那么,您是说只有在aspx页面上定义并使用数据源时才能使用这些值吗?我不确定我是否没有尝试过,但声明说需要在与GridView绑定时使用DataSourceID属性。我可能会尝试一下,看看是否可以为数据源分配一个ID,并将其绑定到代码隐藏中。如果没有,我将尝试下面的建议=)尝试e.Keys而不是gv.Columns[I].ExtractValuesFromCell(e.NewValues,cell,在我的例子中,manuel cell values提取没有改变任何东西:NewValues集合仍然为空。这不能解释为什么旧值为null,因此不是问题的解决方案。但是,随意重新绑定网格是一个常见的错误。
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
    GridBinder()
End Sub