Welcome to Rainbow Portal Community Sign in | Join | Help

Composite Controls And ViewState keys

Reading some of my daily blogs today, I came across a nice post by Scott Guthrie, talking about Composite Controls.

He explained something that I felt was worth mentioning, which I had not previously known.

This is something that is actually going to help me explore some new possibilites in fixing the issues that were there with the SettingWizard actually, so I am quite excited.

In his post he talks about allowing the child controls, to maintain their own properties. So as opposed to storing my own viewstate keys for a number of things like I am used to, he recommends doing it like this...

   1:  public class one
   2:  {
   3:       private TextBox txtFoo;
   4:       public MyControl() 
   5:       {
   6:           this.EnsureChildControls(); 
   7:       } 
   8:       public string Text {
   9:           get { return this.txtFoo.Text; }
  10:           set { this.txtFoo.Text = value; } 
  11:       } 
  12:       protected override CreateChildControls() 
  13:       {
  14:           this.txtFoo = new TextBox();
  15:           this.Controls.Add(txtFoo);
  16:       }
  17:  }
  18:  // -------------------------------
  19:  // As opposed to:
  20:  // -------------------------------
  21:  public class two
  22:  {
  23:       public string Text 
  24:       {
  25:           get { return ViewState["Text"] as string; }
  26:           set { ViewState["Text"] = value; } 
  27:       } 
  28:       public void CreateControlHierarchy() 
  29:       {
  30:           TextBox tb = new TextBox();
  31:           tb.Text = this.Text; 
  32:       }
  33:  }

The reasons for this being sever, again all mentioned at Scott's site

Published mercoledì 15 febbraio 2006 14.28 by Jonathan

Comments

# re: Composite Controls And ViewState keys

mercoledì 15 febbraio 2006 17.58 by ramseur
Its blank for me...You forget the pics...

# re: Composite Controls And ViewState keys

mercoledì 15 febbraio 2006 18.25 by Jonathan
Fixed... there was no images, Community Server bugs are ruining my text formatting that i try to implement on code sections.

# re: Composite Controls And ViewState keys

mercoledì 15 febbraio 2006 20.52 by yogi
Jonathan,
Thanks for sharing this kind of information.
Anonymous comments are disabled