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