A simple composite control
-
Hi All, I'm tring to create a very simple composite control, consisting of a textbox and a label (and some html to style it..) I have created it and at first it seemed to work, but now it appears I have missed out something fundamental. I have a Text property in which I am setting the text property of the child text control and retreiving the same child property.
public string Text
{
get
{
EnsureChildControls();
return childTextBox.Text;
}
set
{
EnsureChildControls();
childTextBox.Text = value;
}
}This works fine, unless on loading the page containing the control, I assign some text to the Text property. The text I assign appears in the child textbox fine, but when edited and attemped to save, it loses the new child textbox value, and reverts to the Text property as it was set on page load (or any other event that I use to populate it). I'm guessing it's some kind of viewstate issue? Though this is the first time I have attempted a custom control of any sort and the help I've been looking up has led to me breaking it completely a number of time while trying various solutions. If anyone has any idea what the problem is, then I'd appreciate your help as I'm running out of time to spend on it and don't want to have to abandon it because of deadlines. Thanks in advance.. Mark
-
Hi All, I'm tring to create a very simple composite control, consisting of a textbox and a label (and some html to style it..) I have created it and at first it seemed to work, but now it appears I have missed out something fundamental. I have a Text property in which I am setting the text property of the child text control and retreiving the same child property.
public string Text
{
get
{
EnsureChildControls();
return childTextBox.Text;
}
set
{
EnsureChildControls();
childTextBox.Text = value;
}
}This works fine, unless on loading the page containing the control, I assign some text to the Text property. The text I assign appears in the child textbox fine, but when edited and attemped to save, it loses the new child textbox value, and reverts to the Text property as it was set on page load (or any other event that I use to populate it). I'm guessing it's some kind of viewstate issue? Though this is the first time I have attempted a custom control of any sort and the help I've been looking up has led to me breaking it completely a number of time while trying various solutions. If anyone has any idea what the problem is, then I'd appreciate your help as I'm running out of time to spend on it and don't want to have to abandon it because of deadlines. Thanks in advance.. Mark
Where are you setting the default value for the textbox? In the Load event of your user control? Have you place that inside
if(!IsPostBack)
block?50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
-
Where are you setting the default value for the textbox? In the Load event of your user control? Have you place that inside
if(!IsPostBack)
block?50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!
Hi, Thanks for the reply. Yes it was in the page load and even though I tried it on other events, I somehow never remembered to remove the onload version of the event, coupled with it not being in a (!Page.IsPostBack) block. Having done that, it works just fine. Thanks for pointing out the error, I'd spent so long looking for problems with the control, I didn't even think it could be with my page :doh: Thanks again Mark