Subproperties

Discussion in 'ASP .Net Building Controls' started by Justin Friel, Apr 1, 2006.

  1. Justin Friel

    Justin Friel Guest

    Hi,

    I've been learning how to create custom web controls and have been
    having problems with subproperties.

    The first problem is that you can not see subproperties using
    intellisense. You can type the subproperty attribute in the tag and it
    will render correctly, but the source view of the page shows a
    validation error. For example:

    <cc1:SubPropertyExample runat="server" textbox-text="Some sample text"
    />

    correctly sets the attribute of the sub property, but I get a
    validation error. Intellisense does not show Textbox-Text as an
    option.

    The second issue is that changes made to the property in design view do
    not stick. If I set the Text property of the Textbox in design view
    the changes are lost as soon as I switch to source view.

    Below is a stripped-down sample control that illustrates the problem.
    If anyone can tell me what I'm doing wrong I'd appreciate it.

    Justin

    [ToolboxData("<{0}:SubPropertyExample
    runat=server></{0}:SubPropertyExample>")]
    public class SubPropertyExample : CompositeControl {
    private TextBox textbox;

    public SubPropertyExample()
    : base() {
    }

    [
    Category("Behavior"),
    Bindable(true),
    Localizable(true),
    NotifyParentProperty(true)
    ]
    public TextBox TextBox {
    get {
    CreateTextBox();
    return textbox;
    }
    }

    protected override void CreateChildControls() {
    base.CreateChildControls();
    this.CreateTextBox();
    }

    protected override void RenderContents(HtmlTextWriter output) {
    }

    private void CreateTextBox() {
    if (textbox == null) {
    textbox = new TextBox();
    textbox.ID = this.GetType().Name;
    }
    }
    }
     
    Justin Friel, Apr 1, 2006
    #1
    1. Advertising

  2. Justin Friel

    Teemu Keiski Guest

    Hi,

    1. Add
    DesignerSerializationVisibility(DesignerSerializationVisibility.Content)
    attribute to the TextBox property, that makes subproperties visible in
    Intellisense

    2. I've faced these composite control's refresh problems, and often the
    reason for their existence is that MS hasn't implemented
    NotifyParentProperty attribute in the so-called simple properties (such as
    TextBox's Text). So you could try simply subclassing TextBox

    public class MyTextBox : TextBox
    {
    [NotifyParentProperty(true) ]
    public override string Text
    {
    get
    {
    return base.Text;
    }
    set
    {
    base.Text = value;
    }
    }

    }

    Change your composite control to use it, and see if it then works for the
    Text property. If it does, then you know that you need to implement it
    yourself for the controls.

    Completely another way is to implement the proeprties yourself (as-
    top-elevl proeprties) and set them to the child control at render time. That
    works, but of course pretty much breaks the idea of using composite controls
    (since you need to implement proeprties anyways yourself)

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke

    "Justin Friel" <> wrote in message
    news:...
    > Hi,
    >
    > I've been learning how to create custom web controls and have been
    > having problems with subproperties.
    >
    > The first problem is that you can not see subproperties using
    > intellisense. You can type the subproperty attribute in the tag and it
    > will render correctly, but the source view of the page shows a
    > validation error. For example:
    >
    > <cc1:SubPropertyExample runat="server" textbox-text="Some sample text"
    > />
    >
    > correctly sets the attribute of the sub property, but I get a
    > validation error. Intellisense does not show Textbox-Text as an
    > option.
    >
    > The second issue is that changes made to the property in design view do
    > not stick. If I set the Text property of the Textbox in design view
    > the changes are lost as soon as I switch to source view.
    >
    > Below is a stripped-down sample control that illustrates the problem.
    > If anyone can tell me what I'm doing wrong I'd appreciate it.
    >
    > Justin
    >
    > [ToolboxData("<{0}:SubPropertyExample
    > runat=server></{0}:SubPropertyExample>")]
    > public class SubPropertyExample : CompositeControl {
    > private TextBox textbox;
    >
    > public SubPropertyExample()
    > : base() {
    > }
    >
    > [
    > Category("Behavior"),
    > Bindable(true),
    > Localizable(true),
    > NotifyParentProperty(true)
    > ]
    > public TextBox TextBox {
    > get {
    > CreateTextBox();
    > return textbox;
    > }
    > }
    >
    > protected override void CreateChildControls() {
    > base.CreateChildControls();
    > this.CreateTextBox();
    > }
    >
    > protected override void RenderContents(HtmlTextWriter output) {
    > }
    >
    > private void CreateTextBox() {
    > if (textbox == null) {
    > textbox = new TextBox();
    > textbox.ID = this.GetType().Name;
    > }
    > }
    > }
    >
     
    Teemu Keiski, Apr 1, 2006
    #2
    1. Advertising

  3. Justin Friel

    Justin Friel Guest

    Adding
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    to the property fixed the Intellisense problem.

    Overriding the text property in an inherited control did not fix the
    issue with values set from the designer not persisting. I'm still not
    sure what's going on with this.

    Thanks for the help,
    Justin
     
    Justin Friel, Apr 1, 2006
    #3
  4. Justin Friel

    Teemu Keiski Guest

    About 2) I forgot to add after adding the new type that change the property
    and CreateTextBox to be like this:

    public TextBox TextBox {
    get {

    EnsureChildControls();
    return textbox;
    }
    }

    private void CreateTextBox() {
    if (textbox == null) {
    textbox = new MyTextBox();
    textbox.ID = this.GetType().Name;
    //Add contrtol to Controls collection
    Controls.Add(textbox);
    }
    }

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke




    "Justin Friel" <> wrote in message
    news:...
    > Adding
    > [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    > to the property fixed the Intellisense problem.
    >
    > Overriding the text property in an inherited control did not fix the
    > issue with values set from the designer not persisting. I'm still not
    > sure what's going on with this.
    >
    > Thanks for the help,
    > Justin
    >
     
    Teemu Keiski, Apr 2, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CaptnKebec

    Collection editor or subproperties

    CaptnKebec, Apr 29, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    129
    CaptnKebec
    Apr 29, 2005
  2. Jason

    DataBinding to SubProperties

    Jason, Jun 21, 2006, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    151
    Walter Wang [MSFT]
    Jun 27, 2006
Loading...

Share This Page