【发布时间】:2009-10-09 14:52:15
【问题描述】:
我的 ASP.Net 页面上有一个 ajax 工具包选项卡容器,我在后面的代码中动态创建选项卡(在运行时,我有可变数量的选项卡,每个选项卡具有相同的布局但不同的标题文本和身体数据)。每个选项卡只包含一个控件,这是我为使所有选项卡看起来都一样而构建的用户控件。在用户控件中是我构建的另一个用户控件,用于处理选项卡内的数据分页。该分页用户控件具有支持其 ViewState 的属性。
我在 ASPX 页面上的单个 BindData 函数中绑定选项卡的所有数据。在该函数中,我总是为我动态创建的控件设置一个 ID,并将它们添加到它们的容器控件中,然后再设置任何其他属性、连接事件或将数据绑定到它们。
在这种情况下,我在分页用户控件中丢失了 ViewState 变量的值:
ASPX 主页面
Page_Init:
If IsPostBack Then
BindData ' Recreates the control tree on postback.
' ViewState is loaded successfully here.
End If
Page_Load:
If Not IsPostBack Then
BindData() ' Bind the initial data.
End If
[Some event that happens after Page_Load in response to clicking on my user control]:
...
BindData() ' Controls recreated with changed data but same IDs as in Page_Init.
' ViewState does not get loaded back into my control tree.
我不应该期望 ViewState 在页面生命周期的第二个版本的控件树上再次加载吗?
【问题讨论】: