如何添加在母版页基地动态HiddenField [英] How To: add dynamically HiddenField in masterpage base page

查看:173
本文介绍了如何添加在母版页基地动态HiddenField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个母版基类,从中我masterpages将继承。我有它的子页面的一些JavaScript函数有包括。因为它是一个基类,它没有一个可视化的设计我也不能添加XHTML code。
我需要一个隐藏字段添加到类,所以我可以设置它在JavaScript的code值,当回发时,我可以让我的内容网页的设定值不变。
然而,我无法实现这一点,当我试图隐藏字段添加到基母版的控件集合我得到(如果在Firefox浏览内容编码错误)渲染错误。如果我尝试作弊,并通过ScriptManager的具有相同的名称在添加控件到控件集合代替注册隐藏字段,以及...我得到的价值为空。
怎样才能做到这一点?


解决方案

 公共类MyBaseMaster
    继承母版    私人小组Page_Init(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Init
      如果没有Page.IsPostBack然后
         Page.ClientScript.RegisterHiddenField(MyHiddenField1,初值)
      万一
    结束小组
末级

您可以通过的Request.Form(MyHiddenField1)(因为它不是一个servercontrol,这不是页面的控制集合的一部分)访问HiddenField的值

MSDN:Htt的prequest.Form-物业

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?

解决方案

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

这篇关于如何添加在母版页基地动态HiddenField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆