Accessing value of a hidden field on Masterpage from codebehind
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");
Elsa
(csharpnewbie) The more I learn .Net, the more there is to learn....
Updated on March 29, 2020Comments
-
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.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 about 11 yearsAre you trying to find the control from the masterpage's codebehind or the child page's codebehind?
-
Elsa about 11 yearsthe child page's codebehind
-
Code Maverick about 11 yearsJust make a readonly property named
Master()
in your child page that returns the masterpage instance and reference it in your child page likeMaster.hdnField
-
Code Maverick about 11 yearsI 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 about 11 yearserror: Unable to cast object of type 'System.Web.UI.WebControls.HiddenField' to type 'System.Web.UI.HtmlControls.HtmlInputHidden'.
-
Elsa about 11 yearsUnbelievable !!! This was a big fat brain fart on my side. Thanks very much for your help.
-
Darren Wainwright about 11 yearsBy 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 about 11 yearsClientIDMode = Static is a .net 4 feature, correct? This project is in VS2008 (ClientIDMode does not show in VS2008 HTML intellisense)
-
Darren Wainwright about 11 yearsSorry, yes, .net 4 and up... :)
-
Netricity about 11 yearsI 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.