Setting Properties in a Composite Control...


S

Steven

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);
}
}
}
 
Ad

Advertisements

A

Alessandro Zifiglio

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
 
A

Alessandro Zifiglio

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 said:
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 said:
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);
}
}
}
 
S

Steven

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 said:
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 said:
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 said:
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);
}
}
}
 
Ad

Advertisements

A

Alessandro Zifiglio

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 said:
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 said:
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:[email protected]
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" <[email protected]> ha scritto nel messaggio
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);
}
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top