HighTechTalks DotNet Forums  

Custom Control As A Composite Control

ASP.net Building Controls microsoft.public.dotnet.framework.aspnet.buildingcontrols


Discuss Custom Control As A Composite Control in the ASP.net Building Controls forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
news.microsoft.com
 
Posts: n/a

Default Custom Control As A Composite Control - 07-06-2006 , 08:02 PM






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.



Reply With Quote
  #2  
Old   
John Saunders
 
Posts: n/a

Default Re: Custom Control As A Composite Control - 07-07-2006 , 06:17 AM






"news.microsoft.com" <tmp (AT) devel (DOT) local> wrote

Quote:
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;}
}

....

Quote:
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




Reply With Quote
  #3  
Old   
sam
 
Posts: n/a

Default Re: Custom Control As A Composite Control - 07-21-2006 , 04:29 PM




And actually doing that is standard best practice.


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.