Custom Control As A Composite Control

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Custom Control As A Composite Control

    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 Guest

  2. Similar Questions and Discussions

    1. Problems in Composite Custom Control
      Hi everybody, I am trying to a build an ASP .Net Custom Control(in C#) which will create a table ( with variable number of rows) dynamically based...
    2. Using XSLT in a custom composite control
      Yes, that says it all. I am trying to use XSLT to render the pre/post parts of the composite control in order to keep the look apart from the...
    3. Possible to create a composite control that has a child control that is a validator that validates the composite control itself?
      I am attempting to create a composite control which has a label, followed by an optional error message, followed by two text boxes. I have...
    4. Validating a custom, composite web control
      Hi I need to create a custom, composite server control which contains a text box that needs to be validated. I need to be able to add external...
    5. Using Table control in a custom composite control. Control does not render properly in design time.
      All, I have written a very simple custom composite control that includes a control of type System.Web.UI.WebControls.Table. The control...
  3. #2

    Default Custom Control As A Composite Control

    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 Guest

  4. #3

    Default Re: Custom Control As A Composite Control

    "news.microsoft.com" <tmp@devel.local> wrote in message
    news:OEZzIEWoGHA.4040@TK2MSFTNGP05.phx.gbl...
    > 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 Guest

  5. #4

    Default Re: Custom Control As A Composite Control


    And actually doing that is standard best practice.

    sam Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139