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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:36:47  来源:igfitidea点击:

Accessing value of a hidden field on Masterpage from codebehind

c#asp.netmaster-pageshidden-field

提问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");但我得到一个“空”值。

enter image description here

在此处输入图片说明

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 HtmlInputHiddenchanged 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 HtmlInputHiddenif 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;
}