Setting Properties in a Composite Control...

Discussion in 'ASP .Net Web Controls' started by Steven, Jul 12, 2006.

  1. Steven

    Steven Guest

    Hello All,

    I have a fairly simple question regarding best practises for Composite
    controls.

    If I want to save into ViewState the data from a postback to a
    ChildControl, I set a property of the Composite Control. This can't be
    done from the CreateChildControl class because the child controls are
    not yet populated with the postback data at this point. I have
    therefore being setting the property that will store this data into
    view state in the event handler for the child control. I have provided
    an example below where the postback data of a text control is saved to
    a property of the Composite Control when the Event Handler of the
    TextBox is called. Is this a suitable way to peform this or is there a
    better practise to use?

    public class CustomTextBox : CompositeControl
    {
    public event EventHandler TextChanged;

    protected Label label;
    protected TextBox textBox;

    public string Title
    {
    get { return (string)ViewState["Title"]; }
    set { ViewState["Title"] = value; }
    }

    public string Text
    {
    get { return (string)ViewState["Text"]; }
    set { ViewState["Text"] = value; }
    }

    protected override void CreateChildControls()
    {
    // Add the label
    label = new Label();
    label.EnableViewState = false;
    label.Text = Title;
    Controls.Add(label);

    // Add the textbox
    textBox = new TextBox();
    textBox.EnableViewState = false;
    textBox.Text = Text;
    textBox.TextChanged += new EventHandler(OnTextChanged);
    Controls.Add(textBox);
    }

    protected virtual void OnTextChanged(object sender, EventArgs
    e)
    {
    if (TextChanged != null)
    {
    // Set the property of the Composite Control to store
    the Text in ViewState
    Text = textBox.Text;
    TextChanged(this, e);
    }
    }
    }
     
    Steven, Jul 12, 2006
    #1
    1. Advertising

  2. hi Steven, viewstate is already maintained by the individual controls that
    you are using (label, textbox). So in your text and title properties, get
    accessor, check if the text of both your label and textbox are not empty and
    if they are not then pull the values from here. Since you are inheriting
    CompositeControls, you shouldnt have a problem because it already ensures
    that CreateChildControls is already called prior to pulling the values from
    the controls.
    Also as you can see, your disabling viewstate on the textbox will produce an
    unwanted result since the textchanged event of the textbox will fire,
    regardless the text has changed or not. This is because since you disabled
    viewstate it does not compare old and new values, not being able to do so,
    it simply fires this event regardless.

    Regards,
    Alessandro Zifiglio
    http://www.AsyncUI.net
    "Steven" <> ha scritto nel messaggio
    news:...
    > Hello All,
    >
    > I have a fairly simple question regarding best practises for Composite
    > controls.
    >
    > If I want to save into ViewState the data from a postback to a
    > ChildControl, I set a property of the Composite Control. This can't be
    > done from the CreateChildControl class because the child controls are
    > not yet populated with the postback data at this point. I have
    > therefore being setting the property that will store this data into
    > view state in the event handler for the child control. I have provided
    > an example below where the postback data of a text control is saved to
    > a property of the Composite Control when the Event Handler of the
    > TextBox is called. Is this a suitable way to peform this or is there a
    > better practise to use?
    >
    > public class CustomTextBox : CompositeControl
    > {
    > public event EventHandler TextChanged;
    >
    > protected Label label;
    > protected TextBox textBox;
    >
    > public string Title
    > {
    > get { return (string)ViewState["Title"]; }
    > set { ViewState["Title"] = value; }
    > }
    >
    > public string Text
    > {
    > get { return (string)ViewState["Text"]; }
    > set { ViewState["Text"] = value; }
    > }
    >
    > protected override void CreateChildControls()
    > {
    > // Add the label
    > label = new Label();
    > label.EnableViewState = false;
    > label.Text = Title;
    > Controls.Add(label);
    >
    > // Add the textbox
    > textBox = new TextBox();
    > textBox.EnableViewState = false;
    > textBox.Text = Text;
    > textBox.TextChanged += new EventHandler(OnTextChanged);
    > Controls.Add(textBox);
    > }
    >
    > protected virtual void OnTextChanged(object sender, EventArgs
    > e)
    > {
    > if (TextChanged != null)
    > {
    > // Set the property of the Composite Control to store
    > the Text in ViewState
    > Text = textBox.Text;
    > TextChanged(this, e);
    > }
    > }
    > }
    >
     
    Alessandro Zifiglio, Jul 12, 2006
    #2
    1. Advertising

  3. Also, try the following link, the code example might help you, though since
    you inherit CompositeControl you might not need to call EnsureChildControls,
    supposedly that is already taken care of for you when you inherit
    CompsiteControl class :
    http://msdn2.microsoft.com/en-us/library/system.web.ui.control.ensurechildcontrols.aspx
    Regards,
    Alessandro Zifiglio
    http://www.AsyncUI.net

    "Alessandro Zifiglio" <AlessandroZifiglio @ -h-o-t-m-a-i-l-c-o-m> ha scritto
    nel messaggio news:...
    > hi Steven, viewstate is already maintained by the individual controls that
    > you are using (label, textbox). So in your text and title properties, get
    > accessor, check if the text of both your label and textbox are not empty
    > and if they are not then pull the values from here. Since you are
    > inheriting CompositeControls, you shouldnt have a problem because it
    > already ensures that CreateChildControls is already called prior to
    > pulling the values from the controls.
    > Also as you can see, your disabling viewstate on the textbox will produce
    > an unwanted result since the textchanged event of the textbox will fire,
    > regardless the text has changed or not. This is because since you disabled
    > viewstate it does not compare old and new values, not being able to do so,
    > it simply fires this event regardless.
    >
    > Regards,
    > Alessandro Zifiglio
    > http://www.AsyncUI.net
    > "Steven" <> ha scritto nel messaggio
    > news:...
    >> Hello All,
    >>
    >> I have a fairly simple question regarding best practises for Composite
    >> controls.
    >>
    >> If I want to save into ViewState the data from a postback to a
    >> ChildControl, I set a property of the Composite Control. This can't be
    >> done from the CreateChildControl class because the child controls are
    >> not yet populated with the postback data at this point. I have
    >> therefore being setting the property that will store this data into
    >> view state in the event handler for the child control. I have provided
    >> an example below where the postback data of a text control is saved to
    >> a property of the Composite Control when the Event Handler of the
    >> TextBox is called. Is this a suitable way to peform this or is there a
    >> better practise to use?
    >>
    >> public class CustomTextBox : CompositeControl
    >> {
    >> public event EventHandler TextChanged;
    >>
    >> protected Label label;
    >> protected TextBox textBox;
    >>
    >> public string Title
    >> {
    >> get { return (string)ViewState["Title"]; }
    >> set { ViewState["Title"] = value; }
    >> }
    >>
    >> public string Text
    >> {
    >> get { return (string)ViewState["Text"]; }
    >> set { ViewState["Text"] = value; }
    >> }
    >>
    >> protected override void CreateChildControls()
    >> {
    >> // Add the label
    >> label = new Label();
    >> label.EnableViewState = false;
    >> label.Text = Title;
    >> Controls.Add(label);
    >>
    >> // Add the textbox
    >> textBox = new TextBox();
    >> textBox.EnableViewState = false;
    >> textBox.Text = Text;
    >> textBox.TextChanged += new EventHandler(OnTextChanged);
    >> Controls.Add(textBox);
    >> }
    >>
    >> protected virtual void OnTextChanged(object sender, EventArgs
    >> e)
    >> {
    >> if (TextChanged != null)
    >> {
    >> // Set the property of the Composite Control to store
    >> the Text in ViewState
    >> Text = textBox.Text;
    >> TextChanged(this, e);
    >> }
    >> }
    >> }
    >>

    >
    >
     
    Alessandro Zifiglio, Jul 12, 2006
    #3
  4. Steven

    Steven Guest

    Thanks for the reply Alessandro,

    My thinking behind disabling View State for the child controls is for
    cases were the composite control contains more complex child controls
    which I thought would bloat the size of the web pages with view state
    data. Then only the important data needs to be persisted in the pages
    ViewState through the set and get calls of the Composite Controls
    properties.

    This introduces the problem you noted of the TextChanged event firing
    everytime the page posts back. This is why I added the code to store
    the text into the Composite Controls properties when the OnTextChanged
    event fires. This may not be a desirable location to set this data
    though, which may mean I will just have to enable View State for the
    child controls and deal with the extra web page size introduced.

    Cheers for the input.


    Alessandro Zifiglio wrote:
    > Also, try the following link, the code example might help you, though since
    > you inherit CompositeControl you might not need to call EnsureChildControls,
    > supposedly that is already taken care of for you when you inherit
    > CompsiteControl class :
    > http://msdn2.microsoft.com/en-us/library/system.web.ui.control.ensurechildcontrols.aspx
    > Regards,
    > Alessandro Zifiglio
    > http://www.AsyncUI.net
    >
    > "Alessandro Zifiglio" <AlessandroZifiglio @ -h-o-t-m-a-i-l-c-o-m> ha scritto
    > nel messaggio news:...
    > > hi Steven, viewstate is already maintained by the individual controls that
    > > you are using (label, textbox). So in your text and title properties, get
    > > accessor, check if the text of both your label and textbox are not empty
    > > and if they are not then pull the values from here. Since you are
    > > inheriting CompositeControls, you shouldnt have a problem because it
    > > already ensures that CreateChildControls is already called prior to
    > > pulling the values from the controls.
    > > Also as you can see, your disabling viewstate on the textbox will produce
    > > an unwanted result since the textchanged event of the textbox will fire,
    > > regardless the text has changed or not. This is because since you disabled
    > > viewstate it does not compare old and new values, not being able to do so,
    > > it simply fires this event regardless.
    > >
    > > Regards,
    > > Alessandro Zifiglio
    > > http://www.AsyncUI.net
    > > "Steven" <> ha scritto nel messaggio
    > > news:...
    > >> Hello All,
    > >>
    > >> I have a fairly simple question regarding best practises for Composite
    > >> controls.
    > >>
    > >> If I want to save into ViewState the data from a postback to a
    > >> ChildControl, I set a property of the Composite Control. This can't be
    > >> done from the CreateChildControl class because the child controls are
    > >> not yet populated with the postback data at this point. I have
    > >> therefore being setting the property that will store this data into
    > >> view state in the event handler for the child control. I have provided
    > >> an example below where the postback data of a text control is saved to
    > >> a property of the Composite Control when the Event Handler of the
    > >> TextBox is called. Is this a suitable way to peform this or is there a
    > >> better practise to use?
    > >>
    > >> public class CustomTextBox : CompositeControl
    > >> {
    > >> public event EventHandler TextChanged;
    > >>
    > >> protected Label label;
    > >> protected TextBox textBox;
    > >>
    > >> public string Title
    > >> {
    > >> get { return (string)ViewState["Title"]; }
    > >> set { ViewState["Title"] = value; }
    > >> }
    > >>
    > >> public string Text
    > >> {
    > >> get { return (string)ViewState["Text"]; }
    > >> set { ViewState["Text"] = value; }
    > >> }
    > >>
    > >> protected override void CreateChildControls()
    > >> {
    > >> // Add the label
    > >> label = new Label();
    > >> label.EnableViewState = false;
    > >> label.Text = Title;
    > >> Controls.Add(label);
    > >>
    > >> // Add the textbox
    > >> textBox = new TextBox();
    > >> textBox.EnableViewState = false;
    > >> textBox.Text = Text;
    > >> textBox.TextChanged += new EventHandler(OnTextChanged);
    > >> Controls.Add(textBox);
    > >> }
    > >>
    > >> protected virtual void OnTextChanged(object sender, EventArgs
    > >> e)
    > >> {
    > >> if (TextChanged != null)
    > >> {
    > >> // Set the property of the Composite Control to store
    > >> the Text in ViewState
    > >> Text = textBox.Text;
    > >> TextChanged(this, e);
    > >> }
    > >> }
    > >> }
    > >>

    > >
    > >
     
    Steven, Jul 13, 2006
    #4
  5. you are welcome, Steven. I see your reasoning behind it. Yes, I also think
    its better to let the controls manage their own viewstate, in this way you
    reduce in code complexity and it will be easier for you to develop and
    maintain it. With viewstate there are always some trade offs. Still, if the
    default clientside viewstate bloat is being a problem for you, you can try
    to store viewstate on the serverside. There are already a few third parties
    that make this easy or you should be able to write a custom solution
    yourself.

    Regards,
    Alessandro Zifiglio
    http://www.AsyncUI.net

    "Steven" <> ha scritto nel messaggio
    news:...
    > Thanks for the reply Alessandro,
    >
    > My thinking behind disabling View State for the child controls is for
    > cases were the composite control contains more complex child controls
    > which I thought would bloat the size of the web pages with view state
    > data. Then only the important data needs to be persisted in the pages
    > ViewState through the set and get calls of the Composite Controls
    > properties.
    >
    > This introduces the problem you noted of the TextChanged event firing
    > everytime the page posts back. This is why I added the code to store
    > the text into the Composite Controls properties when the OnTextChanged
    > event fires. This may not be a desirable location to set this data
    > though, which may mean I will just have to enable View State for the
    > child controls and deal with the extra web page size introduced.
    >
    > Cheers for the input.
    >
    >
    > Alessandro Zifiglio wrote:
    >> Also, try the following link, the code example might help you, though
    >> since
    >> you inherit CompositeControl you might not need to call
    >> EnsureChildControls,
    >> supposedly that is already taken care of for you when you inherit
    >> CompsiteControl class :
    >> http://msdn2.microsoft.com/en-us/library/system.web.ui.control.ensurechildcontrols.aspx
    >> Regards,
    >> Alessandro Zifiglio
    >> http://www.AsyncUI.net
    >>
    >> "Alessandro Zifiglio" <AlessandroZifiglio @ -h-o-t-m-a-i-l-c-o-m> ha
    >> scritto
    >> nel messaggio news:...
    >> > hi Steven, viewstate is already maintained by the individual controls
    >> > that
    >> > you are using (label, textbox). So in your text and title properties,
    >> > get
    >> > accessor, check if the text of both your label and textbox are not
    >> > empty
    >> > and if they are not then pull the values from here. Since you are
    >> > inheriting CompositeControls, you shouldnt have a problem because it
    >> > already ensures that CreateChildControls is already called prior to
    >> > pulling the values from the controls.
    >> > Also as you can see, your disabling viewstate on the textbox will
    >> > produce
    >> > an unwanted result since the textchanged event of the textbox will
    >> > fire,
    >> > regardless the text has changed or not. This is because since you
    >> > disabled
    >> > viewstate it does not compare old and new values, not being able to do
    >> > so,
    >> > it simply fires this event regardless.
    >> >
    >> > Regards,
    >> > Alessandro Zifiglio
    >> > http://www.AsyncUI.net
    >> > "Steven" <> ha scritto nel messaggio
    >> > news:...
    >> >> Hello All,
    >> >>
    >> >> I have a fairly simple question regarding best practises for Composite
    >> >> controls.
    >> >>
    >> >> If I want to save into ViewState the data from a postback to a
    >> >> ChildControl, I set a property of the Composite Control. This can't be
    >> >> done from the CreateChildControl class because the child controls are
    >> >> not yet populated with the postback data at this point. I have
    >> >> therefore being setting the property that will store this data into
    >> >> view state in the event handler for the child control. I have provided
    >> >> an example below where the postback data of a text control is saved to
    >> >> a property of the Composite Control when the Event Handler of the
    >> >> TextBox is called. Is this a suitable way to peform this or is there a
    >> >> better practise to use?
    >> >>
    >> >> public class CustomTextBox : CompositeControl
    >> >> {
    >> >> public event EventHandler TextChanged;
    >> >>
    >> >> protected Label label;
    >> >> protected TextBox textBox;
    >> >>
    >> >> public string Title
    >> >> {
    >> >> get { return (string)ViewState["Title"]; }
    >> >> set { ViewState["Title"] = value; }
    >> >> }
    >> >>
    >> >> public string Text
    >> >> {
    >> >> get { return (string)ViewState["Text"]; }
    >> >> set { ViewState["Text"] = value; }
    >> >> }
    >> >>
    >> >> protected override void CreateChildControls()
    >> >> {
    >> >> // Add the label
    >> >> label = new Label();
    >> >> label.EnableViewState = false;
    >> >> label.Text = Title;
    >> >> Controls.Add(label);
    >> >>
    >> >> // Add the textbox
    >> >> textBox = new TextBox();
    >> >> textBox.EnableViewState = false;
    >> >> textBox.Text = Text;
    >> >> textBox.TextChanged += new EventHandler(OnTextChanged);
    >> >> Controls.Add(textBox);
    >> >> }
    >> >>
    >> >> protected virtual void OnTextChanged(object sender, EventArgs
    >> >> e)
    >> >> {
    >> >> if (TextChanged != null)
    >> >> {
    >> >> // Set the property of the Composite Control to store
    >> >> the Text in ViewState
    >> >> Text = textBox.Text;
    >> >> TextChanged(this, e);
    >> >> }
    >> >> }
    >> >> }
    >> >>
    >> >
    >> >

    >
     
    Alessandro Zifiglio, Jul 13, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sleigh
    Replies:
    1
    Views:
    2,706
    sleigh
    Feb 12, 2004
  2. jeff_suhr
    Replies:
    1
    Views:
    2,423
    jeff_suhr
    Sep 22, 2006
  3. Wardie
    Replies:
    4
    Views:
    500
    Wardie
    Nov 8, 2006
  4. Chad
    Replies:
    0
    Views:
    236
  5. Mike

    Composite vs non composite Controls

    Mike, Mar 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    272
    Sundararajan
    Mar 11, 2005
Loading...

Share This Page