tags:

views:

326

answers:

2

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.

Could someone please help me be sane again? :-)

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: Thank you Vyrotek for looking at it :-) 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?

A: 

I am installing express edition as I type. By that time, why should you clear the controls on click of the button? cant you just add/load the uc once control on click of the link?

By doing this, you would not be clearing the old controls and they would retain the value.

Ramesh
If i type a value in one of the textboxes, its lost on postback.
schmoopy
+2  A: 

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

Vyrotek
thank you, looking now :-)
schmoopy
Thank you very much for taking the time to do that. Your sample works but varies from what im trying to accomplish: i need to add 'new' uc's on the fly, here is a modified version showing what im trying to do http://keithsblogs.com/ControlTest2.zip -- again thanks for taking time :-)
schmoopy
Hi, just saw your edit - if i remove it from the click even, nothing ever shows up on the page.
schmoopy
Just got your code, I'm playing with it :)
Vyrotek
So the issue Im seeing is that whenever LoadSlideControls is called anytime after the click event the textboxes will lose their value. Aps.net is looking for the original textboxes earlier on during the 'PageLifeCycle' and by the time the click even comes around its too late to apply the values.
Vyrotek
Yes, and i dont know what to do about it, -- not sure what i can do at this point tho - kinda sad :-( i wish there was a direct way to call ViewStateDirty() so it would reload anytime in the lifecycle. I have done similiar but i load the controls directly and rename the ID's, not load entire uc's
schmoopy
I think I have it, Im uploading it now. Ill edit my original post.
Vyrotek
Vyrotek, i think you are my hero :-) ... funny thing is, i tiptoed around it kinda - i had changed to loading the controls OnLoad, but EVERYONE including MS always says to do it in the OnInit - the piece i didnt do is i was still calling the full Setup() again after adding to the panel on the click
schmoopy
THANK YOU SOOOOO much - i really appreciate it :-)
schmoopy