开发者

Settings properties of a child in a composite control in an .aspx page

开发者 https://www.devze.com 2023-02-13 18:25 出处:网络
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'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.


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.


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" />


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消