开发者

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.

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜