Custom Control As A Composite Control

  • Thread starter news.microsoft.com
  • Start date
N

news.microsoft.com

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

John Saunders

news.microsoft.com said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,701
Latest member
XavierQ83

Latest Threads

Top