在 Edit/InsertTemplate 工作示例中带有 gridview 的 Asp.net Formview

发布于 2024-12-11 02:34:17 字数 709 浏览 0 评论 0原文

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

某些业务对象具有集合属性,我希望以与单个属性相同的方式编辑/插入这些属性:Text='<%# Bind("SinglePropertyName") %>'

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

这可以很好地显示模板,集合将呈现到网格视图。问题是要对其进行更改。

我尝试这样做,但没有成功,在尝试对 gridview 进行数据绑定时出现以下异常:数据绑定方法,例如 Eval()XPath()Bind() 只能在数据绑定控件的上下文中使用。

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

因此,我想查看类似场景的工作示例,看看我是否能够做到这一点,或者是否需要使用不同的方法。

谢谢!

I'm using a FormView to edit my business objects. I don't have any problem to edit/insert single properties.

Some of the business objects have collection properties that I'd like edit/insert in the same way I do for the single properties: Text='<%# Bind("SinglePropertyName") %>'.

So I'd like to include a gridview inside of the edit/insert templates and bind (two-way) it Datasource to the collection property: Datasource='<%# Bind("CollectionPropertyName") %>'. Then I'd like to be able to edit the collection propties items with the gridview itself and get the changed values among the other sigleproperties' changes.

This works fine to show the template, the collection is rendered to the gridview. The problem is to get the changes on it.

I've tried to do so with no luck, I get the following exception when trying to Databind the gridview: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Beside that, the NewValues of the FormView for the CollectionProperty from the ItemUpdating event always return null.

So I'd like to see a working example of a similar scenario to see if I'm able to do it or if I need to use a different approach.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀手六號 2024-12-18 02:34:17

我已经找到了解决方案,它将 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();
            }
        }
    }

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

I already found a solution to this and it was to encapsulate the gridview in a user control (ObjectList) that exposes a Value property to bind to.

<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();
            }
        }
    }

I hope this help you if you deal with something like that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文