【问题标题】:Asp.net Formview with a gridview inside the Edit/InsertTemplate working example在 Edit/InsertTemplate 工作示例中带有 gridview 的 Asp.net Formview
【发布时间】:2013-08-26 09:42:48
【问题描述】:

我正在使用FormView 来编辑我的业务对象。我编辑/插入单个属性没有任何问题。

一些业务对象具有我想要编辑/插入的集合属性,就像我对单个属性所做的那样:Text='<%# Bind("SinglePropertyName") %>'

所以我想在编辑/插入模板中包含一个网格视图,并将它的数据源绑定(双向)到集合属性:Datasource='<%# Bind("CollectionPropertyName") %>'。然后我希望能够使用 gridview 本身编辑集合属性项,并在其他 sigleproperties 的更改中获取更改后的值。

这可以很好地显示模板,集合被渲染到 gridview。问题是要对其进行更改。

我尝试这样做没有运气,在尝试对 gridview 进行数据绑定时出现以下异常:Eval()XPath()Bind() 等数据绑定方法只能在上下文中使用一个数据绑定控件。

除此之外,ItemUpdating 事件中 CollectionProperty 的 FormView 的 NewValues 始终返回 null。

所以我想看一个类似场景的工作示例,看看我是否能够做到,或者我是否需要使用不同的方法。

谢谢!

【问题讨论】:

    标签: asp.net gridview collections nested formview


    【解决方案1】:

    我已经找到了解决方案,它是将 gridview 封装在一个用户控件 (ObjectList) 中,该控件公开一个要绑定的 Value 属性。

    <uc:ObjectList ID="ucObjectList" runat="server" Value='<%#Bind("Items") %>' />
    

    ObjectList.ascx:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ObjectList.ascx.cs" Inherits="TestBinding.ObjectList" %>
    
        <asp:GridView runat="server" ID="grdItems" DataSource='<%#Datasource%>' 
        OnRowEditing="grdItems_RowEditing" 
        OnRowCancelingEdit="grdItems_RowCancelingEdit" 
        OnRowUpdating="grdItems_OnRowUpdating">
        <Columns>
        <asp:CommandField ShowEditButton="True"></asp:CommandField>
        </Columns>
        </asp:GridView>
    

    ObjectList.ascx.cs:

    
    
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Web.UI.WebControls;
    
        namespace TestBinding
        {
            public partial class ObjectList : UserControl
            {
                protected List Datasource
                {
                    get
                    {
                        if (ViewState["ObjectList"] == null) ViewState["ObjectList"] = new Test();
                        return (List)ViewState["ObjectList"];
                    }
                    set { ViewState["ObjectList"] = value; }
                }
    
                [Bindable(true, BindingDirection.TwoWay)]
                public List Value
                {
                    get { return Datasource; }
    
                    set { Datasource = value; }
                }
    
                protected void Page_Load(object sender, EventArgs e)
                {
    
                }
    
                protected void grdItems_RowEditing(object sender, GridViewEditEventArgs e)
                {
                    ((GridView)sender).EditIndex = e.NewEditIndex;
                    ((GridView)sender).DataSource = Datasource;
                    ((GridView)sender).DataBind();
                }
    
                protected void grdItems_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
                {
                    ((GridView)sender).EditIndex = -1;
                    ((GridView)sender).DataSource = Datasource;
                    ((GridView)sender).DataBind();
                }
    
                protected void grdItems_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
                {
                    Datasource[e.RowIndex].ID = int.Parse(e.NewValues["ID"].ToString());
                    Datasource[e.RowIndex].Last = (string)e.NewValues["Last"];
                    ((GridView)sender).EditIndex = -1;
                    ((GridView)sender).DataSource = Datasource;
                    ((GridView)sender).DataBind();
                }
            }
        }
    
    

    如果您处理类似的事情,我希望这对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 2014-07-12
      • 2018-06-24
      • 1970-01-01
      相关资源
      最近更新 更多