Search code examples
asp.netsharepointuser-controlssharepoint-2007web-parts

User Control on a WebPart, PostBack / ViewState Problem


I'm using a Sharepoint WebPart to load a UserControl which has a button that does some processing on PostBack. I got a problem: when I click the button for the first time, the data loaded on ! IsPosback gets lost, but this does not occur when I click the button again. I think my problem is explained here: Sharepoint Lifecycle, but I haven't been able to find a workaround.

Any help would be really appreciated.

Additional Info: I'm using EnsureChildControls on the WebPart's OnLoad event, and loading the UserControl on CreateChildControls.


Solution

  • I was able to fix this by programatically specifying an ID to the User Control.

    E.g.:

    protected void Page_Load(object sender, EventArgs e)
    {
        this.ID = "MyUserControlID";
    }
    

    More info here: http://bytes.com/topic/asp-net/answers/314816-dynamically-loaded-control-event-only-reached-2nd-postback