开发者

Persisting html-control content with runat server tag

开发者 https://www.devze.com 2023-02-03 19:42 出处:网络
I have this problem - I\'m working on an ASP.NET AJAX-application on a server without .net 3.5 which means I can\'t use ListView (BOO!).

I have this problem - I'm working on an ASP.NET AJAX-application on a server without .net 3.5 which means I can't use ListView (BOO!).

I decided to simply do the ugly way of making a table and giving the tbody an ID and a runat server tag, and put HtmlControls inside from the codebehind (btw - this is a usercontrol, in case it makes a difference).

This all works fine - until the page does a partial postback - and all the elements disappear, since the tag is runat server I assume it requires to be repopulated in the page load.

Is there no way to actually persist the data on partial postback? And only have the content of the control change when I say so in the back 开发者_如何学Cend?

I guess I could save the content in a session object upon populating the control, and just repopulate in the Page_Load with the session object - but I was hoping there was a better way to do this?


You should recreate all controls created dynamically in the Page_Init event and repopulate them in Page_Load. You can refer to this article also https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx.


If your problem is with saving states. The ControlState is just the right place to do so..

Consider this from msdn (http://msdn.microsoft.com/en-us/library/1whwt1k7.aspx)

or try the following code.

    protected override object SaveControlState()
    {
        object[] ControlStateData = new object[];
        //save your data like  ControlStateData[0] = ActivePage;
        return ControlStateData;
    }

    protected override void LoadControlState(object savedState)
    {
        object[] ControlStateData = savedState as object[];
        //Load your data like ActivePage = (int) ControlStateData[0];
        base.LoadControlState(ControlStateData[2]);
    }

    //Dont forget to add this procedure
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        Page.RegisterRequiresControlState(this);
    }
0

精彩评论

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