views:

2467

answers:

3

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.

+4  A: 

Would something like this work?

((HiddenField)this.Page.Master.FindControl("[hidden control id]")).Text
Jared
Gives me an error "Control is a type and cannot be used as an expression.
Batuta
sorry missed a cast. I edited my answer to correct
Jared
A: 

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.

Canavar
I am not sure I do understand you correctly, but as I have mentioned, I need to get a value of a hidden field in a master page from a class which I created inside app_code folder. How do I access the master page from the class I defined?
Batuta
all these codes returns your hidden field's value after a postback if your hidden field has an ID. you do not need to access masterpage. masterpage is simply a base page for your page class. just try one of these, you will see. But you can get after postback.
Canavar
A: 

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.

Marcus L