在回发时保留动态用户控制
我将用户控件动态加载到 div 中,我需要在回发时保留该控件,以便在用户完成编辑后调用 Save
方法。 div 和所有用户控件都有 EnableViewState = True
。
ASPX Div 声明
<div id="dynamicDiv" runat="server" enableviewstate="true">
</div>
代码隐藏
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not IsPostBack) Then
'Implement some logic to load the User Controls using LoadControl
'Set all the userControl properties (including EnableViewState = True)
'Call a method in the user control to load it's content
Me.dynamicDiv.Controls.add(userControl)
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
For Each t As userControlType In Me.dynamicDiv.Controls
t.Save()
Next
End Sub
所以...控件加载得很好,但我找不到在回发时保留它们的方法,我无法重新加载它们,因为用户已经将数据输入到用户控件中(这是我需要保存的)。
PS 我尝试将它们添加到列表中,然后将该列表添加到 ViewState
中,但我无法正确序列化控件。我为 userControl 的代码实现了 ISerialized
,但随后它说 ASCX 未使用
Type 'ASP.controls_userControlType_ascx' in Assembly 'App_Web_pn5vxhpw, Version=0.0.0.0, Culture=neutral 标记为可序列化, PublicKeyToken=null' 未标记为可序列化。
I'm dynamically loading User Controls into a div, which I need to preserve on postback in order to call a Save
method once the user is done editing them. The div and all the User Controls have the EnableViewState = True
.
ASPX Div Declaration
<div id="dynamicDiv" runat="server" enableviewstate="true">
</div>
Code Behind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not IsPostBack) Then
'Implement some logic to load the User Controls using LoadControl
'Set all the userControl properties (including EnableViewState = True)
'Call a method in the user control to load it's content
Me.dynamicDiv.Controls.add(userControl)
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
For Each t As userControlType In Me.dynamicDiv.Controls
t.Save()
Next
End Sub
So... the controls load fine, but I can't find a way to preserver them on postback, I can't reload them because the user has already input data into the usercontrol (which is what I need to save).
P.S. I tried adding them to a list and then adding the list to the ViewState
, but I haven't been able to properly serialize the control. I implemented ISerializable
for the userControl's code behind but then it says the ASCX is mot marked as serializable with
Type 'ASP.controls_userControlType_ascx' in Assembly 'App_Web_pn5vxhpw, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
动态控件需要在页面初始化而不是页面加载期间添加。然后它们将被视图状态拾取。而且每次都需要创建它们。创建动态控件时不需要 Ispostback。
Dynamic controls need to be added durning page init not page load. Then they will be picked up by viewstate. And they need to be created every time. Ispostback is not needed when creating dynamic controls.
从评论中复制:
如果
IsPostback
,您还需要重新创建 UserControls。您只需分配与之前相同的 ID,最晚将其添加到
Page_Load
中即可。如果您知道要创建的控件数量(可以存储在 ViewState 中),则可以通过将计数器变量附加到控件 ID 来从计数器变量中派生 ID。Copied from comments:
You need to recreate your UserControls also if
IsPostback
.You only need to assign the same IDs as before and add them in
Page_Load
at the latest. If you know the number of controls to create(which could be stored inViewState
) you can derive the ID from the counter variable by appending it to the control-id.