OnRowUpdating 事件中GridView 的NewValues 和OldValues 为空 [英] GridView's NewValues and OldValues empty in the OnRowUpdating event

查看:17
本文介绍了OnRowUpdating 事件中GridView 的NewValues 和OldValues 为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的 GridView.我在后面的代码中绑定到自定义数据源.它进入OnRowUpdating"事件就好了,但没有 NewValues 或 OldValues.关于如何获得这些值的任何建议?

I have the GridView below. I am binding to a custom datasource in the code behind. It gets into the "OnRowUpdating" event just fine, but there are no NewValues or OldValues. Any suggestions as to how I can get these values?

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

推荐答案

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

Regarding on the GridView control's RowUpdating event problem, it is the expected behavior because when we do not associate GridView(or other ASP.NET 2.0 databound control) with DataSource control, it won't automatically query and fill the parameters collection of the updating/deleting/... events. In such cases, we need to manually extract the field values from the Template control.

这是一位微软员工在 此处.

This is what says a Microsoft employee in here.

在这种情况下,您可以使用 ExtractValuesFromCell 方法来自己制作 NewValues 集合.

In that case you can do it using the ExtractValuesFromCell method to make the NewValues collection yourself.

我在这个 博客:

protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{     
       GridView gv = (GridView)sender;
       gv.EditIndex = e.NewEditIndex;
       gv.DataBind();
       ...
}

protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
   GridView gv = (GridView)sender;
   for (int i = 0; i < gv.Columns.Count; i++)
   {
      DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
      gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
   }
   // now you can use NewValues collection normally
}

尚未测试,但似乎解决了问题,如果解决了请告诉我.

Havent tested it, but seems to solve the problem, let me know if it did.

这篇关于OnRowUpdating 事件中GridView 的NewValues 和OldValues 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆