问题描述
在跟进我的previous ,我想获得隐藏的输入字段的值从子页面codebehind。
In a follow up to my previous question, I want to get the value of the hidden input field from the child page codebehind.
我试过 HtmlInputHidden hdnID =(HtmlInputHidden)Page.Master.FindControl(ctl00_hdnField);
但我得到一个空的价值。
I tried HtmlInputHidden hdnID = (HtmlInputHidden)Page.Master.FindControl("ctl00_hdnField");
but I get a "null" value.
母版的片段是:
<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>
在我的孩子aspx页面,我有这个JavaScript块:
On my Child aspx page, I have this javascript block:
window.onload = function() {
var newDate = new Date();
var hidField = document.getElementById("ctl00_hdnField");
if (hidField != null)
hidField.value = newDate.toLocaleString();
}
当我添加监视来
的document.getElementById(ctl00_hdnField)
该值是正确的。
问:我将如何访问hdnField控制里面的价值,从codebehind
Question: How would I access the value inside hdnField control, from codebehind?
推荐答案
所以,改变它
HtmlInputHidden hdnID =(HtmlInputHidden)Page.Master.FindControl(ctl00_hdnField);
到
HiddenField hdnID =(HiddenField)Page.Master.FindControl(hdnField);
这只是一个铸件的事情 - 通知 HtmlInputHidden
更改为 HiddenField
。你也不必在 CT100 _
的一部分 - 这只是让HTML渲染元素都有一个唯一的ID
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.
在网页上的控制是一个asp.net的控制,而不是一个普通的HTML控件。
The control on your page is an asp.net control, not a generic HTML control.
您会使用 HtmlInputHidden
如果你把一个普通的&LT;输入类型=隐藏/&GT;
中你的HTML。
You would use HtmlInputHidden
if you put a generic <input type="hidden" />
in your HTML.
这篇关于从codebehind母版上访问隐藏字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!