如何获得HiddenField控件在母版,并设置它的价值呢? [英] How to get HiddenField control in Masterpage and set it's value?

查看:132
本文介绍了如何获得HiddenField控件在母版,并设置它的价值呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个母版包含一个隐藏字段控制。我想要得到的隐藏字段的当前值,并从使用母版页面设置它的价值。

I have a MasterPage that contains a hidden field control. I want to get the current value of the hidden field and set the value of it from pages that use the MasterPage.

我有以下的code到目前为止(在一个页面中)

I have the following code so far: (in one of the pages)

   //Get the textbox and set it's value
   TextBox txt1 = new TextBox();
                txt1 = (TextBox)this.Master.FindControl("txtHiddenField");
                txt1 .Text = "true";

以上code似乎没有工作。我需要什么code,以获得隐藏字段控制和设置它的价值呢? (得到它的值)

The above code does not seem to work. What code would I need to get the hidden field control and set it's value? (and get it's value)

推荐答案

我会建议提供一个公共属性/方法的母版,你可以用它来设置/获取HiddenField的值。

I would recommend to provide a public property/method in your MasterPage, that you can use to set/get the HiddenField's value.

在你的法师(假设它的类型叫做 SITEMASTER

in your Master(assuming it's type is called SiteMaster):

public String HiddenValue { 
    get{return txtHiddenField.Value;}
    set{txtHiddenField.Value = value;}
}

在你的页面:

SiteMaster master = (SiteMaster)Page.Master;
master.HiddenValue = "true";

这方法是直接的,不易出错,容易阅读。你甚至可以改变控制在你的主人,而无需更改页面(f.e。如果你想更换一个TextBox隐藏字段)。

This approach is straight-forward, less prone to errors and easily readable. You could even change the control in your master without needing to change the pages(f.e. if you want to replace the hidden-field with a TextBox).

假设你的真实值表明你真的要存储一个布尔值,我会建议使用布尔作为数据类型的属性和一个不言自明的名称。然后就可以将其存储在hiddenfield但客户端(页)不需要知道

Assuming that your "true" value indicates that you actually want to store a boolean, i would recommend to use a bool as data-type for the property and a self-explanatory name. Then you can store it in the hiddenfield but the client(the page) does not need to know.

这篇关于如何获得HiddenField控件在母版,并设置它的价值呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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