Question

Is there a way to get a value I am storing in a Master Page hidden field from a User Class which I created and placed in the App_Code folder of my ASP.Net 2.0 Application?

Some examples would preferably in VB.Net is highly appreciated.

Thanks.

To give further details, assume the following:

MasterPage.Master MasterPage.Master.vb

MyPage.aspx Mypage.aspx.vb

IN the app_code folder, add a new class, say TESTClass.

I have placed some logic in master page. MyPage.aspx uses the Masterpage.master as its master page. In the master page, the logic which I did stores a value into a hidden field.

in my TestClass, how do I access the master page hidden field?

Please take note that TestClass is NOT a user control but a user defined class, which contains some Business-Specific logic which is accessed by myPage.aspx.vb.

I tried ScarletGarden's suggestion but it did not seem to get the Masterpage Hiddenfield which I need to get the value.

Was it helpful?

Solution

Would something like this work?

((HiddenField)this.Page.Master.FindControl("[hidden control id]")).Text

OTHER TIPS

You can get it by these :

hiddenControlValue = HttpContext.Current.Request["hiddenControlId"]

or you can pass your page to your method that belongs to your class under App_Config, and reach it as :

public static string GetHiddenValue(Page currentPage)
{
        return currentPage.Request["hiddenValue"];
}

or you can get it over context :

public static string GetHiddenValue()
{
        return HttpContext.Current.Request["hiddenValue"];
}

hope this helps.

EDIT: I re-read the question after answering, and realize my answer was probably not quite what you were after. :/

Jared's code might work, but you can also try the following.

In your MasterPage, make the HiddenField a public property, and store the content in the ViewState to make keep it during post backs.

Something like so:

public HiddenField theHiddenField
{
    get
    {
        if (ViewState["HiddenField"] == null)
            return null; //or something that makes you handle an unset ViewState
        else
            return ViewState["HiddenField"].ToString();
    }
    set
    {
        ViewState["HiddenField"] = value;
    }
}

You then have to add the following to your ASCX-file:

<%@ Reference Control="~/Masterpages/Communication.Master" %>

You then access it thusly.

Page mypage = (Page) this.Page; // Or instead of Page, use the page you're actually working with, like MyWebsite.Pages.PageWithUserControl
MasterPage mp = (MasterPage) mypage.Master;
HiddenField hf = mp.theHiddenField;

Sorry if the answer got a bit messy. This is, of course, how to do it in C#, if you want to use VB have a look at this link for the same idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top