Custom Control As A Composite Control

Discussion in 'ASP .Net Building Controls' started by news.microsoft.com, Jul 7, 2006.

  1. Hi,
    I'm building a custom control and I have an issue. My custom control has a
    (Server) TextBox control in it and my custom contol exposes a property named
    Text. I want my Text property displayed within my TextBox (So I basically
    set TextBox.Text property = Text (that is custom control's Text property) in
    my CreateChildControls method. But in design mode when I set my (custom
    control's) Text property the change is not reflected to Text property of my
    TextBox. What is the most efficient way to achieve this? (I guess calling
    EnsureChildControls in custom control's Text property set accessor is not a
    very good idea and sometimes it works sometimes not.) Also I am not
    overriding Render method. Psuedo-like code follows:

    // Some Attributes...
    public class MyControl: WebControl, INamingContainer
    ....
    public string Text
    {
    get
    {
    return ( ViewState["Text"] == null ? String.Empty :
    ViewState["Text"] )
    }
    set
    {
    ViewState["Text"] = value;
    }
    }

    protected override void CreateChildControls()
    {
    base.CreateChildControls();
    TextBox textBox = new TextBox();
    textBox.ID = "myTextBox";
    textBox.Text = this.Text;
    this.Controls.Add(textBox);
    this.ChildControlsCreated = true;
    }

    When I change Text property in design-mode textBox.Text does not change
    until page is refreshed (in design-mode).

    I will appreciate suggestions or best practices to achieve this. Thanks in
    advance.
    news.microsoft.com, Jul 7, 2006
    #1
    1. Advertising

  2. "news.microsoft.com" <> wrote in message
    news:...
    > Hi,
    > I'm building a custom control and I have an issue. My custom control has a
    > (Server) TextBox control in it and my custom contol exposes a property
    > named
    > Text. I want my Text property displayed within my TextBox (So I basically
    > set TextBox.Text property = Text (that is custom control's Text property)
    > in
    > my CreateChildControls method. But in design mode when I set my (custom
    > control's) Text property the change is not reflected to Text property of
    > my
    > TextBox. What is the most efficient way to achieve this? (I guess calling
    > EnsureChildControls in custom control's Text property set accessor is not
    > a
    > very good idea and sometimes it works sometimes not.)


    Calling EnsureChildControls in the Text property is exactly what I'd try:

    public string Text
    {
    get {EnsureChildControls(); return textBox.Text;}
    set {EnsureChildControls(); textBox.Text = value;}
    }

    ....

    > protected override void CreateChildControls()
    > {
    > base.CreateChildControls();
    > TextBox textBox = new TextBox();
    > textBox.ID = "myTextBox";
    > textBox.Text = this.Text;
    > this.Controls.Add(textBox);
    > this.ChildControlsCreated = true;
    > }


    If you don't call EnsureChildControls earlier, it will be called in
    PreRender, which is likely to be too late.

    John
    John Saunders, Jul 7, 2006
    #2
    1. Advertising

  3. news.microsoft.com

    sam Guest

    And actually doing that is standard best practice.
    sam, Jul 21, 2006
    #3
    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. Replies:
    0
    Views:
    707
  2. sleigh
    Replies:
    1
    Views:
    2,666
    sleigh
    Feb 12, 2004
  3. Replies:
    1
    Views:
    358
    Teemu Keiski
    Jul 2, 2003
  4. Chad
    Replies:
    0
    Views:
    209
  5. Mike

    Composite vs non composite Controls

    Mike, Mar 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    249
    Sundararajan
    Mar 11, 2005
Loading...

Share This Page