|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Setting Properties in a Composite Control...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); } } } 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 Show quoteHide quote "Steven" <steven***@gmail.com> ha scritto nel messaggio news:1152690634.983346.142530@i42g2000cwa.googlegroups.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); > } > } > } > 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 Show quoteHide quote "Alessandro Zifiglio" <AlessandroZifiglio @ -h-o-t-m-a-i-l-c-o-m> ha scritto nel messaggio news:eHClqTZpGHA.756@TK2MSFTNGP05.phx.gbl... > 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" <steven***@gmail.com> ha scritto nel messaggio > news:1152690634.983346.142530@i42g2000cwa.googlegroups.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); >> } >> } >> } >> > > 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: Show quoteHide quote > 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:eHClqTZpGHA.756@TK2MSFTNGP05.phx.gbl... > > 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" <steven***@gmail.com> ha scritto nel messaggio > > news:1152690634.983346.142530@i42g2000cwa.googlegroups.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); > >> } > >> } > >> } > >> > > > > 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 Show quoteHide quote "Steven" <steven***@gmail.com> ha scritto nel messaggio news:1152750256.243939.142670@75g2000cwc.googlegroups.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/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:eHClqTZpGHA.756@TK2MSFTNGP05.phx.gbl... >> > 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" <steven***@gmail.com> ha scritto nel messaggio >> > news:1152690634.983346.142530@i42g2000cwa.googlegroups.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); >> >> } >> >> } >> >> } >> >> >> > >> > >
Repeater and text boxes inside
how to change labelvalue when deleting row in gridview? Multi-line textbox scrolled to bottom? Problem handling event in a button inside a Datalist Can't install iewebcontrols.msi - error "webctrl_client is unavailable" How to put an image in a GridView textfield not recognized in gridview menu control with XML file passing variable to sql statement in asp.net 2.0? creative scroll bar for asp.net |
|||||||||||||||||||||||