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

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

问题描述

下面我有GridView控件。我在code后面绑定到一个自定义的数据源。它得到进入OnRowUpdating事件就好了,但目前还没有NewValues​​或OldValues​​。任何建议,我怎么能获得这些价值?

 < ASP:GridView控件ID =gv_Personnel
                        =服务器
                        OnRowDataBound =gv_Personnel_DataBind
                        OnRowCancelingEdit =gv_Personnel_CancelEdit
                        OnRowEditing =gv_Personnel_EditRow
                        OnRowUpdating =gv_Personnel_UpdateRow
                        的AutoGenerateColumns =假
                        ShowFooter =真
                        的DataKeyNames =BudgetLineID
                        AutoGenerateEditButton属性=真
                        AutoGenerateDeleteButton =真
                        >
            <柱体和GT;
                < ASP:BoundField的的HeaderText =员工的级别数据字段=LineDescription/>
                <% - < ASP:BoundField的的HeaderText =小时/单位要求的数据字段=NumberOfUnits/> - %GT;
                < ASP:的TemplateField的HeaderText =小时/单位申请>
                    <&ItemTemplate中GT;
                        <%#的eval(NumberOfUnits)%>
                    < / ItemTemplate中>
                    <&EditItemTemplate的GT;
                        < ASP:文本框ID =tb_NumUnits=服务器文本='<%#绑定(NumberOfUnits)%>' />
                    < / EditItemTemplate中>
                < / ASP:的TemplateField>
                < ASP:BoundField的的HeaderText =申请人成本分担小时/单位数据字段=NullDisplayText =0/>
                < ASP:BoundField的的HeaderText =合作伙伴成本分担小时/单位数据字段=NullDisplayText =0/>
                < ASP:BoundField的FooterStyle-字体粗体=真FooterText =TOTAL人才服务的HeaderText =率DataFormatString ={0:C}数据字段=单价/>
                < ASP:的TemplateField的HeaderText =申请金额ItemStyle-Horizo​​ntalAlign =右FooterStyle-Horizo​​ntalAlign =右FooterStyle-边框宽度=2FooterStyle-字体粗体=真/>
                < ASP:的TemplateField的HeaderText =申请者的成本共享ItemStyle-Horizo​​ntalAlign =右FooterStyle-Horizo​​ntalAlign =右FooterStyle-边框宽度=2FooterStyle-字体粗体=真/>
                < ASP:的TemplateField的HeaderText =合作伙伴成本分担ItemStyle-Horizo​​ntalAlign =右FooterStyle-Horizo​​ntalAlign =右FooterStyle-边框宽度=2FooterStyle-字体粗体=真/>
                < ASP:的TemplateField的HeaderText =总成本谟ItemStyle-Horizo​​ntalAlign =右FooterStyle-Horizo​​ntalAlign =右FooterStyle-边框宽度=2FooterStyle-字体粗体=真/>
            < /专栏>
        < / ASP:GridView的>


解决方案

  

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


这是说,在<一个微软员工href=\"http://www.dotnetmonster.com/Uwe/Forum.aspx/asp-net-datagrid/3487/GridView-DataSource-RowUpdating-e-OldValues-and-e-NewValues\"相对=nofollow>这里。

在这种情况下,你可以用它做的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datacontrolfield.extractvaluesfromcell.aspx\"相对=nofollow> ExtractValues​​FromCell 方法,使NewValues​​收集自己。

编辑:

我发现了一张code在的<一个评论href=\"http://bloggingabout.net/blogs/dennis/archive/2006/08/24/DataBinding-in-ASP.NET-2.0-and-the-RowUpdating-event.aspx\"相对=nofollow>这个博客:

 保护无效OnRowEditing(对象发件人,GridViewEditEventArgs E)
{
       GridView控件GV =(GridView的)发件人;
       gv.EditIndex = e.NewEditIndex;
       gv.DataBind();
       ...
}保护无效OnRowUpdating(对象发件人,GridViewUpdateEventArgs E)
{
   GridView控件GV =(GridView的)发件人;
   的for(int i = 0; I&LT; gv.Columns.Count;我++)
   {
      DataControlFieldCell电池= gv.Rows [e.RowIndex] .Cells [I]作为DataControlFieldCell;
      gv.Columns [I] .ExtractValues​​FromCell(e.NewValues​​,细胞,DataControlRowState.Edit,真正的);
   }
   //现在就可以正常使用NewValues​​集合
}

还没有测试它,但似乎解决了问题,让我知道,如果它没有。

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>

解决方案

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.

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

EDIT:

I found a piece of code in the comments of this blog:

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.

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

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