Search code examples
asp.netdynamichiddenmaster-pagesradix

How To: add dynamically HiddenField in masterpage base page


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?


Solution

  • 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