Accessing value of a hidden field on Masterpage from codebehind

23,937

Solution 1

So change it FROM

HtmlInputHidden hdnID = (HtmlInputHidden)Page.Master.FindControl("ctl00_hdnField");

TO

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.

The control on your page is an asp.net control, not a generic HTML control.

You would use HtmlInputHidden if you put a generic <input type="hidden" /> in your HTML.

Solution 2

You should create a property in Masterpage which wrap the 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.

Solution 3

I don't think you need to prefix the hidden field's ID with ctl00_, just use the normal ID:

(HtmlInputHidden)Page.Master.FindControl("hdnField");
Share:
23,937
Elsa
Author by

Elsa

(csharpnewbie) The more I learn .Net, the more there is to learn....

Updated on March 29, 2020

Comments

  • Elsa
    Elsa about 4 years

    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.

    enter image description here

    A snippet of the Masterpage is:

    <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:

    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")

    the value is correct.

    Question: How would I access the value inside hdnField control, from codebehind?

    • Code Maverick
      Code Maverick about 11 years
      Are you trying to find the control from the masterpage's codebehind or the child page's codebehind?
    • Elsa
      Elsa about 11 years
      the child page's codebehind
    • Code Maverick
      Code Maverick about 11 years
      Just make a readonly property named Master() in your child page that returns the masterpage instance and reference it in your child page like Master.hdnField
    • Code Maverick
      Code Maverick about 11 years
      I would note that you would probably need to go into your designer file and make hdnField public. But, that's the cleanest and simplest solution.
  • Elsa
    Elsa about 11 years
    error: Unable to cast object of type 'System.Web.UI.WebControls.HiddenField' to type 'System.Web.UI.HtmlControls.HtmlInputHidden'.
  • Elsa
    Elsa about 11 years
    Unbelievable !!! This was a big fat brain fart on my side. Thanks very much for your help.
  • Darren Wainwright
    Darren Wainwright about 11 years
    By the way, if you're planning on using any Javascript to interact with the form elements and your using .net 3.5 or higher then also set the ClientIDMode=Static property - this will prevent .net from adding all sorts of nonsense to your ID's.
  • Elsa
    Elsa about 11 years
    ClientIDMode = Static is a .net 4 feature, correct? This project is in VS2008 (ClientIDMode does not show in VS2008 HTML intellisense)
  • Darren Wainwright
    Darren Wainwright about 11 years
    Sorry, yes, .net 4 and up... :)
  • Netricity
    Netricity about 11 years
    I just used 'HtmlInputHidden' because that's what you had at the top of your own example. Obviously you should be casting to HiddenField as Darren suggests.