开发者

User Control on a WebPart, PostBack / ViewState Problem

开发者 https://www.devze.com 2023-01-08 04:20 出处:网络
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 ! IsPosb

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.

Additi开发者_如何学Goonal Info: I'm using EnsureChildControls on the WebPart's OnLoad event, and loading the UserControl on CreateChildControls.


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


protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    if (ViewState["MyStuff"] == null)
        LoadMyStuffAndSaveToViewState();
    else
        DoSomethingWith(ViewState["MyStuff"]);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消