自定义模板化 ASP.NET 控件的双向数据绑定

2024-05-22

这个问题最初是关于让双向绑定工作的,但由于缺乏具体的答案和其他方面的进展,我一直在更新它 - 你可以检查编辑历史记录,但我认为这对于明晰。

下面列出的代码允许将单个对象双向数据绑定到模板化控件。我想以最简单的方式扩展此示例,以允许针对最根对象的复杂类型属性嵌套类似的双向数据绑定启用模板化控件。例如,SampleFormData有财产List<string> Items。我希望能够绑定到最根模板中的此列表(从此代码清单中),并且可以在可编辑的文本框列表中显示字符串数据,也许可以使用插入、删除、重新绑定输入的命令-changes(返回到绑定对象的 List 属性)。另外,如果这是一个复杂类型的列表(SampleFormChildData,而不是字符串),一个新的嵌入SampleSpecificEntryForm可以在列表中使用,绑定到列表的每个项目,就像转发器一样。依此类推,直至叶子简单属性(如果作者如此选择)。 ui 字段不需要自动生成,只需用于绑定即可。

注:案例如下List<string>很特殊,因为即使内置绑定也无法直接将字符串作为 DataItem 处理 - 直接将字符串作为列表中的项目绑定不是必需的,但肯定很有价值。

这不同于FormView因为它不是为了期望绑定到项目列表之一而构建的,而只是绑定到在视图状态或任何地方持久存在的单个项目。与 FormView 不同,它只有一个类似于 FormView 的 EditTemplate 的默认模板。同样,绑定到类似集合的属性也只有一个视图 - 编辑。没有选择行然后进行编辑。一切都可以随时编辑。目的是使双向绑定表单更容易构建。

在我看来,应该有两种绑定。SingleEntityBinding and CollectionBinding. SingleEntityBinding将单个对象实例作为数据源(原型为SampleSpecificEntryForm) while CollectionBinding可以绑定到它的父级SingleEntityBinding具有以下属性DataSourceID="EntryForm1" DataMember="Items"如代码示例中所示DataList1以下。任一类型都应支持任一类型的嵌套。对支持对象数据的列表操作(例如插入/更改/删除类型操作)是表单作者的责任;然而,这种机制实施起来相对简单。

这是一些代码,希望对某人有所帮助。针对这个既定目标的最佳建议有 200 分……

using System.ComponentModel;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace System.Web.UI.WebControls.Special
{
    [Serializable]
    public class SampleFormData
    {
        public string SampleString { get; set; }
        public int SampleInt { get; set; }
        public List<string> Items { get; set; }

        public SampleFormData()
        {
            SampleString = "Sample String Data";
            SampleInt = 5;
            Items = new List<string>();
        }
    }

    [ToolboxItem(false)]
    public class SampleSpecificFormDataContainer : WebControl, INamingContainer, IDataItemContainer
    {
        SampleSpecificEntryForm entryForm;

        internal SampleSpecificEntryForm EntryForm
        {
            get { return entryForm; }
        }

        [Bindable(true), Category("Data")]
        public string SampleString
        {
            get { return entryForm.FormData.SampleString; }
            set { entryForm.FormData.SampleString = value; }
        }

        [Bindable(true), Category("Data")]
        public int SampleInt
        {
            get { return entryForm.FormData.SampleInt; }
            set { entryForm.FormData.SampleInt = value; }
        }

        [Bindable(true), Category("Data")]
        public List<string> Items
        {
            get { return entryForm.FormData.Items; }
            set { entryForm.FormData.Items = value; }
        }

        internal SampleSpecificFormDataContainer(SampleSpecificEntryForm entryForm)
        {
            this.entryForm = entryForm;
        }

        #region IDataItemContainer Members
        public object DataItem { get { return entryForm.FormData; } }

        public int DataItemIndex { get { return 0; } }

        public int DisplayIndex { get { return 0; } }
        #endregion
    }

    public class SampleSpecificEntryForm : DataBoundControl, INamingContainer, IDataSource
    {
        #region Template
        private IBindableTemplate formTemplate = null;

        [Browsable(false), DefaultValue(null),
        TemplateContainer(typeof(SampleSpecificFormDataContainer), ComponentModel.BindingDirection.TwoWay),
        PersistenceMode(PersistenceMode.InnerProperty)]
        public virtual IBindableTemplate FormTemplate
        {
            get { return formTemplate; }
            set { formTemplate = value; }
        }
        #endregion

        public override ControlCollection Controls
        {
            get
            {
                EnsureChildControls();
                return base.Controls;
            }
        }

        private SampleSpecificFormDataContainer formDataContainer = null;

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public SampleSpecificFormDataContainer FormDataContainer
        {
            get
            {
                EnsureChildControls();
                return formDataContainer;
            }
        }

        [Bindable(true), Browsable(false)]
        public SampleFormData FormData
        {
            get
            {
                SampleFormData data = ViewState["FormData"] as SampleFormData;

                if (data == null)
                {
                    data = new SampleFormData();
                    ViewState["FormData"] = data;
                }

                return data;
            }
        }

        protected override void CreateChildControls()
        {
            if (!this.ChildControlsCreated)
            {
                this.ChildControlsCreated = true;
                Controls.Clear();
                formDataContainer = new SampleSpecificFormDataContainer(this);

                Controls.Add(formDataContainer);
                FormTemplate.InstantiateIn(formDataContainer);
            }
        }

        protected override void PerformDataBinding(Collections.IEnumerable ignore)
        {
            CreateChildControls();

            if (Page.IsPostBack)
            {
                //OrderedDictionary fields = new OrderedDictionary();

                //ExtractValuesFromBindableControls(fields, formDataContainer); // Don't know what this would be for

                foreach (System.Collections.DictionaryEntry entry in formTemplate.ExtractValues(formDataContainer))
                {
                    if (((string)entry.Key).Equals("SampleString", StringComparison.Ordinal))
                    {
                        FormData.SampleString = (string)entry.Value;
                    }

                    if (((string)entry.Key).Equals("SampleInt", StringComparison.Ordinal))
                    {
                        int i;
                        if (int.TryParse((string)entry.Value, out i))
                        {
                            FormData.SampleInt = i;
                        }
                    }
                }
            }

            formDataContainer.DataBind();
        }

        public SampleSpecificEntryForm()
        {
            this.PreRender += new EventHandler(SampleSpecificEntryForm_PreRender);
        }

        void SampleSpecificEntryForm_PreRender(object sender, EventArgs e)
        {
            SaveViewState();
        }

        #region IDataSource Members

        public event EventHandler DataSourceChanged;

        public DataSourceView GetView(string viewName)
        {
            return new PropertyView(this, viewName);
        }

        public Collections.ICollection GetViewNames()
        {
            return new List<string>() { "SampleString", "SampleInt", "Items" };
        }

        #endregion
    }

    // Not yet used ...
    public class PropertyView : DataSourceView
    {
        SampleSpecificEntryForm owner;
        string viewName;

        protected override Collections.IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
        {
            if (viewName.Equals("SampleString", StringComparison.Ordinal))
            {
                return new object[] { owner.FormData.SampleString };
            }

            if (viewName.Equals("SampleInt", StringComparison.Ordinal))
            {
                return new object[] { owner.FormData.SampleInt };
            }

            if (viewName.Equals("Items", StringComparison.Ordinal))
            {
                return new object[] { owner.FormData.Items };
            }

            throw new InvalidOperationException();
        }

        public PropertyView(SampleSpecificEntryForm owner, string viewName)
            : base(owner, viewName)
        {
            this.owner = owner;
            this.viewName = viewName;
        }
    }
}

对于 ASP.NET 页面,如下所示:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default2.aspx.cs" Inherits="EntryFormTest._Default2" EnableEventValidation="false" %>

<%@ Register Assembly="EntryForm" Namespace="System.Web.UI.WebControls.Special" TagPrefix="cc1" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
    </h2>
        <cc1:SampleSpecificEntryForm ID="EntryForm1" runat="server">
    <FormTemplate>
        <asp:TextBox ID="txtSampleString" runat="server" Text='<%# Bind("SampleString") %>'></asp:TextBox><br />
        <asp:TextBox ID="txtSampleInt" runat="server" Text='<%# Bind("SampleInt") %>'></asp:TextBox><br />
        <h3>
            (<%# Container.SampleString %>, <%# Container.SampleInt %>) - aka - 
            (<%# DataBinder.Eval(Container, "SampleString")%>, <%# DataBinder.Eval(Container, "SampleInt")%>)</h3>
        <br />
        <asp:Button ID="btnUpdate" runat="server" Text="Update" /><br />
        <br />
    </FormTemplate>
</cc1:SampleSpecificEntryForm>
</asp:Content>

默认2.aspx.cs:

using System;

namespace EntryFormTest
{
    public partial class _Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            EntryForm1.DataBind();
        }
    }
}

我也实现了 IDataSource,试图能够像这样嵌套一个列表组件(在 内):

<asp:DataList ID="DataList1" runat="server" DataSourceID="EntryForm1" DataMember="Items">
    <EditItemTemplate>
        <asp:TextBox ID="TextBox3" runat="server" Text="<%# Bind(".") %>"></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
        <asp:Button ID="Button2" runat="server" Text="Add" CommandName="Add" />
    </FooterTemplate>
</asp:DataList>

任何有关如何以级联方式实现此工作的想法都很棒(例如,在“项目”列表属性上)。这里的挑战之一是 Bind() 不能引用数据绑定对象本身(在本例中为字符串),而是引用该项目的属性 - 使得绑定到列表变得很尴尬。

谢谢你的帮助!


一路上的发现

实现了 IDataItemContainer。我非常希望这能解决这个问题,但没有。没有明显的变化。 糟糕,在错误的类上实现了它。现在它是绑定,但值不会在回发时反弹到绑定对象。嗯...

作为本文 http://aspadvice.com/blogs/joteke/archive/2008/08/24/Is-_2200_Databinding-methods-such-as-Eval_280029002C00_-XPath_280029002C00_-and-Bind_28002900_-can-only-be-used-in-the-context-of-a-databound-control_2E002200_-causing-you-grief_3F00_.aspx表明,Page.GetDataItem() 是异常的来源。如果页面的 _dataBindingContext 为 null 或为空,则会引发此异常。文章确实解释了这一点,但没有说明如何确保填充 Page 的 _dataBindingContext。我会继续寻找。

正如 MSDN 文档所述,DataBoundControl 应该实现 PerformDataBinding 而不是重写 DataBind()。我已经这样做了,并进行了双向绑定工作。这段代码是必要的还是我应该使用内置的东西?


你有没有尝试过Databinder.Eval(Container.DataItem,...) https://weblogs.asp.net/rajbk/Contents/Item/Display/1125 syntax?

另请参阅这篇文章Bind() http://weblogs.asp.net/leftslipper/archive/2007/06/29/how-asp-net-databinding-deals-with-eval-and-bind-statements.aspx.

附言。除非您使用 Viewstate 来保留值,否则您需要在每次回发时进行数据绑定。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自定义模板化 ASP.NET 控件的双向数据绑定 的相关文章