设置 .aspx 页面中复合控件中子控件的属性

发布于 2024-10-19 17:50:21 字数 381 浏览 6 评论 0原文

我有一个复合控件 (class),它通过 get-property 公开 asp:Label是否可以通过aspx代码设置标签的Text属性?

我想做这样的事情:

一种解决方案是将每个属性添加到复合类(如公共 LabelText),但我想设置任何子控件的任何属性。因此,随着子控件的新功能变得可用,我希望能够在我的复合控件上使用它们。所以基本上我想设置暴露的子控件的任何属性

I've got a composite control (class) that exposes an asp:Label through a get-property. Is is possible to set the Text property of the Label through aspx-code?

I'd like to do something like this:
<cfw:MyCompositeControl runat="server" Label.Text="Test" />

One solution is adding each property to the composite class (like a public LabelText), but I'd like to set any property of any child control. So as new features of child controls become available, I'd like to be able to use them on my composite control. So basically I would like to set any property of the exposed child control.

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

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

发布评论

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

评论(3

静赏你的温柔 2024-10-26 17:50:21

您可以使用内部属性来完成此操作:

[ParseChildren(ChildrenAsProperties = true)]
public partial class MyControl: UserControl
{

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public TestClass TestLabel
    {
         get;set;
    }
}
public class TestClass
{
    public string Field1
    { get; set; }
    public string Field2
    { get; set; }
}

标记:

<MyTag:MyControl runat="server">
    <TestLabel Field1="a" Field2="b" />
</MyTag:MyControl>

我以前从未使用简单的属性真正完成过此操作 - 通常,您使用的是集合。在我自己尝试这个示例时,在标记窗口中,Visual Studio 将允许您在 内创建多个 TestLabel 实例 - 即是的,它似乎并不关心它是一个简单的属性而不是一个集合,我怀疑如果您输入多个条目,则只会产生最后一个条目。

仅供参考...如果您以前没有这样做过,请准备好被智能感知惹恼。在对类进行更改后更新行为可能会令人烦恼地迟钝,您将需要重新编译,并且可能需要等待一段任意时间才能使其按预期方式运行。

You could do it with inner properties:

[ParseChildren(ChildrenAsProperties = true)]
public partial class MyControl: UserControl
{

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public TestClass TestLabel
    {
         get;set;
    }
}
public class TestClass
{
    public string Field1
    { get; set; }
    public string Field2
    { get; set; }
}

Markup:

<MyTag:MyControl runat="server">
    <TestLabel Field1="a" Field2="b" />
</MyTag:MyControl>

I've never actually done this with a simple property before - usually, you are using collections. In playing with this example on my own, in the markup window, Visual Studio will allow you to create more than one instance of TestLabel inside <MyTag:MyControl> - that is, it doesn't seem to care that it's a simple property rather than a collection, I suspect if you put in more than one entry just the last one would result.

FYI... if you haven't done this before, prepare to be annoyed by intellisense. It can be annoyingly obtuse about updating the behavior after you make changes to a class, you will need to recompile and probably wait for some arbitrary amount of time before it acts the way it's supposed to.

冬天的雪花 2024-10-26 17:50:21

您需要将其公开为复合控件类中的属性:-

public string LabelText
{
  get
  {
    return Label.Text;
  }
  set
  {
    Label.Text = value;
  }
}

然后您可以从服务器标记控制它,例如:-

<cfw:MyCompositeControl runat="server" LabelText="Test" />

You need to expose it as a property in the composite control's class:-

public string LabelText
{
  get
  {
    return Label.Text;
  }
  set
  {
    Label.Text = value;
  }
}

Then you can control it from the server tag like:-

<cfw:MyCompositeControl runat="server" LabelText="Test" />
泪是无色的血 2024-10-26 17:50:21

您应该公开一个返回/设置标签的 Text 属性的公共属性。
MSDN

编辑
不建议您从页面访问复合控件的所有子控件的想法:

  • 一种方法是通过 MyCompositeControl.FindControl(ID) (或扩展方法)访问子控件,这将是非常糟糕的如果您想删除控件或更改 ID,则它是静态的且容易出错。
  • 另一种方法是公开子控件,但这也是一个糟糕的设计,因为它为滥用控件打开了大门,并且如果您想更改子控件并且页面已经直接访问它们,也会出现问题。

You should expose a public property that returns/sets the Text property of the Label.
MSDN

Edit:
Your idea to access all child controls of the composite control from the page is not recommended:

  • One approach would be to access the child controls via MyCompositeControl.FindControl(ID) (or extension methods) what would be very static and error-prone if you want to remove controls or change the IDs.
  • Another approach would be to make the child controls public but that is also bad design because it opens the door for misusing your control and would be also problematic if you want to change the childcontrols and pages are already accessing them directly.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文