C# 从代码隐藏访问 Masterpage 上隐藏字段的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15689859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing value of a hidden field on Masterpage from codebehind
提问by Csharp
In a follow up to my previous question, I want to get the value of the hidden input field from the child page codebehind.
在对我之前的问题的跟进中,我想从子页面代码隐藏中获取隐藏输入字段的值。
I tried HtmlInputHidden hdnID = (HtmlInputHidden)Page.Master.FindControl("ctl00_hdnField");
but I get a "null" value.
我试过了,HtmlInputHidden hdnID = (HtmlInputHidden)Page.Master.FindControl("ctl00_hdnField");
但我得到一个“空”值。
A snippet of the Masterpage is:
Masterpage 的一个片段是:
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server"></asp:ContentPlaceHolder>
<asp:Literal ID="Literal2" runat="server" Text=" : Logistics Management" />
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="~/css/styles.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="ScriptCssContent" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
......
......
......
<div id="container">
....
....
....
<div id="content" style="z-index:0;">
<asp:HiddenField ID="hdnField" runat="server" Value=""/>
....
....
....
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
</div>
</form>
On my Child aspx page, I have this javascript block:
在我的 Child aspx 页面上,我有这个 javascript 块:
window.onload = function() {
var newDate = new Date();
var hidField = document.getElementById("ctl00_hdnField");
if (hidField != null)
hidField.value = newDate.toLocaleString();
}
When I "Add Watch" to
当我“添加监视”到
document.getElementById("ctl00_hdnField")
document.getElementById("ctl00_hdnField")
the value is correct.
值是正确的。
Question:How would I access the value inside hdnField control, from codebehind?
问题:如何从代码隐藏访问 hdnField 控件中的值?
采纳答案by Darren Wainwright
So change it FROM
所以改变它从
HtmlInputHidden hdnID = (HtmlInputHidden)Page.Master.FindControl("ctl00_hdnField");
HtmlInputHidden hdnID = (HtmlInputHidden)Page.Master.FindControl("ctl00_hdnField");
TO
到
HiddenField hdnID = (HiddenField)Page.Master.FindControl("hdnField");
HiddenField hdnID = (HiddenField)Page.Master.FindControl("hdnField");
It's just a casting thing - notice HtmlInputHidden
changed to HiddenField
. You also don't need the ct100_
part - this is just so the HTML rendered element has a unique ID.
这只是一个铸造的东西 - 通知HtmlInputHidden
改为HiddenField
. 您也不需要该ct100_
部分 - 这只是为了让 HTML 呈现的元素具有唯一的 ID。
The control on your page is an asp.net control, not a generic HTML control.
您页面上的控件是一个 asp.net 控件,而不是一个通用的 HTML 控件。
You would use HtmlInputHidden
if you put a generic <input type="hidden" />
in your HTML.
HtmlInputHidden
如果您<input type="hidden" />
在 HTML 中放置泛型,您将使用。
回答by Marseld
You should create a property in Masterpage which wrap the HiddenField.
您应该在 Masterpage 中创建一个包含 HiddenField 的属性。
public String HdnFieldValue
{
get
{
return hidField.Value;
}
set
{
hidField.Value = value;
}
}
And in page code behind you can access it like this:
在后面的页面代码中,您可以像这样访问它:
((YourCustomMaster)Page.Master).HdnFieldValue
If something is not clear please ask me.
如果有什么不清楚的请问我。
回答by Netricity
I don't think you need to prefix the hidden field's ID with ctl00_
, just use the normal ID:
我认为您不需要在隐藏字段的 IDctl00_
前面加上 前缀,只需使用普通 ID:
(HtmlInputHidden)Page.Master.FindControl("hdnField");
回答by Jason Buell
Use something like:
使用类似的东西:
if (Page.Master.FindControl("hdnField") != null)
{
String myValue = (HtmlInputHidden)Page.Master.FindControl("hdnField").value;
}