How To: add dynamically HiddenField in masterpage base page

淺唱寂寞╮ 提交于 2019-12-08 02:20:31

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/7935573/how-to-add-dynamically-hiddenfield-in-masterpage-base-page

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