I have tried what seems like everything – I’ve done similiar things many times before, but I’m obviously missing something.
I have a UserControl (ucA) – on ucA is a LinkButton that loads a different UserControl (ucB) programatically into a panel on ucA. ucB has TextBoxes, etc.
Why isn’t my control maintaining it’s state? ie: The textboxes are loosing thier value on postback – the control tree shows the name of the control and the Form values show the value (in trace.axd)
Here is the code for ucA (basically)
public int SlideCount { get { return Convert.ToInt32(ViewState['SlideCount']); } set { ViewState['SlideCount'] = value; } } protected void LinkButton1_Click(object sender, EventArgs e) { SlideCount += 1; LoadSlideControls(); } protected override void OnInit(EventArgs e) { base.OnInit(e); LoadSlideControls(); } private void LoadSlideControls() { this.pnlAnnouncementHolder.Controls.Clear(); for (int i = 0; i < SlideCount; i++) { this.pnlAnnouncementHolder.Controls.Add( LoadControl('AnnouncementSlide.ascx')); } }
Heres a full example of what im trying to do: http://keithsblogs.com/ControlTest2.zip
The problem Vyrotek points out is that the control is added a little to late to the lifecycle on the click event – anyone have any ideas?
I just tried to recreate what you have setup and I dont seem to experience the same problem. Would you like me to send you my Solution somehow?
Final Edit, I promised –
Try this solution: http://www.vyrotek.com/code/ControlTest2.zip