Viewstate of ascx lost between postbacks

半世苍凉 提交于 2019-12-03 15:31:58

Take a look at the ASP.NET Page Life Cycle and Understanding View State. View State gets loaded after Initialization, so you won't be able to access it in Page_Init. You'd be better off using a hidden field.

If you are dead set on using View State, the earliest you can get to it would be by overriding the LoadViewState method (Remember to call base.LoadViewState before trying to access it though).

Jon444

You also need to add the control to the controls collection BEFORE you set the property. ViewState does not get recorded until after it is added to the controls collection.

placeholder.Controls.Add(ctrl); 
((ControlType)ctrl).SomeProperty = someData; 

Keep track of the ID of the UserControl before postback then on postback re-create the control and assign the ID back and it should automatically load the ViewState back in.

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