asp.net – 在OnRowUpdating事件中,GridView的NewValues和OldValues为空

前端之家收集整理的这篇文章主要介绍了asp.net – 在OnRowUpdating事件中,GridView的NewValues和OldValues为空前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有下面的GridView.我绑定到后面的代码中的自定义数据源.它进入“OnRowUpdating”事件就好了,但没有NewValues或OldValues.关于如何获得这些价值的任何建议?
<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>

解决方法

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.

这就是here年微软员工所说的话.

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

编辑:

我在this博客评论中找到了一段代码

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
}

没有对它进行测试,但似乎解决了这个问题,如果有的话请告诉我.

原文链接:https://www.f2er.com/aspnet/251089.html

猜你在找的asp.Net相关文章