Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Building Controls / July 2006

Tip: Looking for answers? Try searching our database.

Custom Control As A Composite Control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
news.microsoft.com - 07 Jul 2006 02:02 GMT
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.
John Saunders - 07 Jul 2006 12:17 GMT
> Hi,
> I'm building a custom control and I have an issue. My custom control has a
[quoted text clipped - 10 lines]
> 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()
> {
[quoted text clipped - 5 lines]
>    this.ChildControlsCreated = true;
> }

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

John
sam - 21 Jul 2006 22:29 GMT
And actually doing that is standard best practice.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.