在回发时保留动态用户控制

发布于 2025-01-02 20:03:41 字数 1314 浏览 4 评论 0原文

我将用户控件动态加载到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

静若繁花 2025-01-09 20:03:41

动态控件需要在页面初始化而不是页面加载期间添加。然后它们将被视图状态拾取。而且每次都需要创建它们。创建动态控件时不需要 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.

紫南 2025-01-09 20:03:41

从评论中复制:

如果 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 in ViewState) you can derive the ID from the counter variable by appending it to the control-id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文