Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > Custom Control As A Composite Control

Reply
Thread Tools

Custom Control As A Composite Control

 
 
news.microsoft.com
Guest
Posts: n/a
 
      07-07-2006
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
 
 
 
 
John Saunders
Guest
Posts: n/a
 
      07-07-2006
"news.microsoft.com" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
sam
Guest
Posts: n/a
 
      07-21-2006

And actually doing that is standard best practice.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Losing Composite Control property that another Composite Control ... Chad ASP .Net Building Controls 0 02-01-2005 09:01 PM
Possible to create a composite control that has a child control that is a validator that validates the composite control itself? Jonathan Eric Miller ASP .Net Building Controls 2 07-22-2004 10:58 PM
Composite control with dynamic composite controls sleigh ASP .Net 1 02-12-2004 06:24 PM
Using Table control in a custom composite control. Control does not render properly in design time. jb_in_marietta@yahoo.com ASP .Net Building Controls 1 07-02-2003 08:05 AM
Using Table control in a custom composite control. Control does not render properly in design time. jb_in_marietta@yahoo.com ASP .Net 0 07-01-2003 09:26 PM



Advertisments