如何:在母版页基页中动态添加 HiddenField

发布于 2024-12-12 09:02:36 字数 319 浏览 0 评论 0原文

我有一个 Base MasterPage 类,我的母版页将从该类继承。我有一些 javascript 函数供其子页面包含。由于它是一个基类,因此它没有可视化设计器,我也无法添加 XHTML 代码。 我需要向类添加一个隐藏字段,以便我可以在 javascript 代码中设置它的值,并且当发生回发时我可以在我的内容页面上获取设置的值。 然而我未能实现这一点,因为当我尝试将隐藏字段添加到基本母版页的控件集合中时,我收到渲染错误(如果在 Firefox 中查看,则出现内容编码错误)。如果我尝试作弊并通过脚本管理器注册具有相同名称的隐藏字段,而不是将控件添加到控件集合中,那么......我得到的值为空。 怎样才能做到这一点呢?

I have a Base MasterPage class, from which my masterpages will inherit. I have some javascript functions there for it's child pages to include. As it's a base class, it does not have a visual designer nor I can add XHTML code.
I need to add a hidden field to the class so I can set it's value in the javascript code, and when a postback occurs I can get the setted value on my content pages.
Yet I fail to achieve this, for when I try to add the hidden field to the base masterpage's control collection I get a render error (Content Encoding error if viewed in Firefox). And If I try cheating and registering a hidden field via scriptmanager with the same name in stead of adding the control to the control collection, well... I get the value as empty.
How could achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

喜你已久 2024-12-19 09:02:36
Public Class MyBaseMaster
    Inherits MasterPage

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
      If Not Page.IsPostBack Then
         Page.ClientScript.RegisterHiddenField("MyHiddenField1", "initialvalue")
      End If
    End Sub
End Class

您可以通过 Request.Form("MyHiddenField1") 访问 HiddenField 的值(因为它不是服务器控件,所以它不是页面控件集合的一部分)。

MSDN:HttpRequest.Form-Property

Public Class MyBaseMaster
    Inherits MasterPage

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
      If Not Page.IsPostBack Then
         Page.ClientScript.RegisterHiddenField("MyHiddenField1", "initialvalue")
      End If
    End Sub
End Class

You can access the HiddenField's value via Request.Form("MyHiddenField1") (since it's not a servercontrol, it isn't part of the page's control-collection).

MSDN: HttpRequest.Form-Property

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