LoadViewState not fired on my user control

泄露秘密 提交于 2019-12-04 04:27:34

问题


I have a user control nested in a repeater. Inside my user control I have another repeater and in that I have a panel.

I am trying to override the LoadViewState event of my user control and dynamically add controls to the panel. I want to do it in the LoadViewState so that the dynamic controls get added before the viewstate gets loaded, so they retain their values after post backs.

For some reason the LoadViewState event on the user control (ascx) is not firing. Is there some way to force it to fire, or is there another method I could use? I have ruled out the user controls repeater databind event, because I need it to work even if data binding isn't happening and I can't do it on the repeaters item created event either because the child panel and inner html doesn't exist yet.


回答1:


LoadViewState isn't the appropriate place for adding child controls. For dynamically adding controls within a user control, you'll want to look at the CreateChildControls method.

It's not firing a LoadViewState event because you need to save at least one value in the ViewState to have the event fire.




回答2:


I think I had a similar problem with some dynamically created children user controls. LoadViewState wasn't called in postbacks even if I was able to access their ViewState when creating them first. SaveViewState seemed to be also called correctly. It ended that the children ViewState was not really usable (without this resulting in an exception) in the page Init event before they were fully initializated, and this happened only when the controls were added to the parent. After having ensured this, the children ViewState was correctly persisted across postbacks.

    // Belongs to a Page. If you create the children control in the
    // Load event in you can also access the page ViewState
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            for (int it = 0; it < 5; it++)
            {
                ChildControl child = LoadControl("ChildControl.ascx")
                    as ChildControl;
                child.ParentPage = this;
                TabPanel tab = tabContainer.FindControl("TabPanel" + it)
                    as TabPanel;
                // Ensure to add the child control to its parent before
                // accessing its ViewState!
                tab.Controls.Add(child);     // <---
                string caption = "Ciao" + it;
                child.Caption = caption;     // Here we access the ViewState
                tab.HeaderText = caption;
                tab.Visible = true;
                _Children.Add(child);
            }
        }
        [...]
    }

    // Belongs to ChildControl 
    public string Caption
    {
        get { return ViewState["Caption"] as string; }
        internal set { this.ViewState["Caption"] = value; }
    }


来源:https://stackoverflow.com/questions/2478509/loadviewstate-not-fired-on-my-user-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!