ASP.NET/C# - Getting value of HiddenField control from Master Page

1.1k views Asked by At

I have the following HiddenField controls on my client pages:

<asp:HiddenField ID="hidRecordEditMode" runat="server" />
<asp:HiddenField ID="hidRecordEditId" runat="server" />

I am trying to access their value from a method located on my master page, using this code (sample):

protected string GetValue()
{
Page page = (Page)HttpContext.Current.Handler;
Control ctrlEditId;
ctrlEditId = (HiddenField)page.FindControl("hidRecordEditId");
return ctrlEditId.Value;
}

I'm being told the Value property doesn't exist. I've tried with and without casting (HiddenField), and setting the method static, to no avail.

How can I get this to work?

1

There are 1 answers

1
Scotty On
protected string GetValue()
{
    var hfEditId = (HiddenField)ContentPlaceHolder1.FindControl("hidRecordEditId");
    return hfEditId != null ? hfEditId.Value : string.Empty;
}

Where ContentPlaceHolder1 is the ID of the ContentPlaceHolder displaying your content page.