Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Setting Properties in a Composite Control...

Reply
Thread Tools

Setting Properties in a Composite Control...

 
 
Steven
Guest
Posts: n/a
 
      07-12-2006
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);
}
}
}

 
Reply With Quote
 
 
 
 
Alessandro Zifiglio
Guest
Posts: n/a
 
      07-12-2006
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" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed) oups.com...
> 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);
> }
> }
> }
>



 
Reply With Quote
 
 
 
 
Alessandro Zifiglio
Guest
Posts: n/a
 
      07-12-2006
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/lib...dcontrols.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:(E-Mail Removed)...
> 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" <(E-Mail Removed)> ha scritto nel messaggio
> news:(E-Mail Removed) oups.com...
>> 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);
>> }
>> }
>> }
>>

>
>



 
Reply With Quote
 
Steven
Guest
Posts: n/a
 
      07-13-2006
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/lib...dcontrols.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:(E-Mail Removed)...
> > 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" <(E-Mail Removed)> ha scritto nel messaggio
> > news:(E-Mail Removed) oups.com...
> >> 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);
> >> }
> >> }
> >> }
> >>

> >
> >


 
Reply With Quote
 
Alessandro Zifiglio
Guest
Posts: n/a
 
      07-13-2006
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" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed) ups.com...
> 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/lib...dcontrols.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:(E-Mail Removed)...
>> > 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" <(E-Mail Removed)> ha scritto nel messaggio
>> > news:(E-Mail Removed) oups.com...
>> >> 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);
>> >> }
>> >> }
>> >> }
>> >>
>> >
>> >

>



 
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
Setting properties (not necessarily "Properties") in Java 5.0 Wardie Java 4 11-08-2006 09:46 AM
Composite vs non composite Controls Mike ASP .Net Web Controls 4 03-11-2005 05:47 AM
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



Advertisments