【问题标题】:Usercontrol in EditItemTemplate of templated control. How to achieve two-way databinding?模板化控件的 EditItemTemplate 中的用户控件。如何实现双向数据绑定?
【发布时间】:2014-11-11 19:54:55
【问题描述】:

我希望将 ASP.NET ListView 的 EditItemTemplate 替换为用户控件,但我不知道如何将其绑定到 ListView 中的数据项以实现双向数据绑定。数据项不是一个简单的属性。它是一个对象。 这似乎只给出了单向的 Eval 类型数据绑定:

<EditItemTemplate>
    <uc:MyUserControl id="thecontrol" runat="server" TheObject='<%# Container.DataItem %>'/>
</EditItemTemplate>

这给出了一个错误:

<EditItemTemplate>
    <uc:MyUserControl id="thecontrol" runat="server" TheObject='<%# Bind("Container.DataItem") %>'/>
</EditItemTemplate>

这样:

<EditItemTemplate>
    <uc:MyUserControl id="thecontrol" runat="server" TheObject='<%# Bind("this") %>'/>
</EditItemTemplate>

是否有一些绑定表达式语法可以为 EditItemTemplate 中的当前项提供双向数据绑定?

编辑。这是用户控件:

public partial class EditItemUserControl : System.Web.UI.UserControl
    {
        public TestObject TheObject
        {
            get
            {
                return new TestObject() { ID = Int32.Parse(hfID.Value), Name = txtName.Text };
            }
            set
            {
                hfID.Value = value.ID.ToString();
                txtName.Text = value.Name;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }

【问题讨论】:

    标签: asp.net webforms


    【解决方案1】:

    仅供参考。我做错了。用户控件位于呈现它的页面的上下文中,因此无需在其上设置 DataSource。在 .ascx 文件中,您可以使用:

    Text = '<%# Bind("MyProperty") %>'
    

    Text = '<%# Eval("MyProperty") %>'
    

    其中 MyProperty 是父控件的 DataSource 的某个属性。

    【讨论】:

      猜你喜欢
      • 2019-10-26
      • 2011-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 2011-03-03
      • 2015-10-27
      相关资源
      最近更新 更多