Take the 2-minute tour ×

Attempting something like

public string MyText { get {return lbl.Text;} set {lbl.Text = value;}}

taken from this question ends in an exception:

public string change_ddlArea { get { return ddlArea.SelectedItem.Text; } set { ddlArea.Items.FindByValue(value).Selected = true; } }


Using these normally cause the same error:

this.ddlEnergyType.Items.FindByValue(resourceTypeVal).Selected = true;
((DropDownList)Master.FindControl("ddlArea")).Items.FindByValue("all").Selected = true;

I'm having difficulty finding an answer to this problem on google. It should work, given if a label manipulation does, moving to a ddl would as well?

The one difference that could be the problem is when trying to call this.. (in the 2nd code block) I use

Master.change_ddlArea = sendoff_area;

and in the aformentioned link it is done like so

((MasterPageTypeName)Page.MasterPage ).MyText = "test";
//so mine would look like this:
((GAWMaster)_default.Master).change_ddlArea = "all";

however intellisense is missing the '.Master' - putting it in causes the information to appear: an object reference is required for the non-static field, method or property....

tl,dr: halp

share|improve this question
Silly question, but are you sure your page derives from a Master Page? –  icemanind Jul 6 '11 at 18:30

2 Answers 2

up vote 1 down vote accepted

Your question is VERY confusing to say the least. It's very simple to do what your title is asking:

First in your MasterPage code behind do:

public string change_ddlArea
    get { return ddlArea.SelectedItem.Value; }
    set { ddlArea.Items.FindByValue(value).Selected = true; }

Then in your child page define your MasterPage type so that it can 'see' the change_ddlArea property of the MasterPage. See here for more reference info.

<%@ MasterType virtualPath="~/MasterPage.master"%>

Then in your code on your child page you should be able to just do:


The value you are setting MUST exist or you will get an object reference error. Make sure your DropDownList is loaded correctly and you are not doing things out of order. Also, if your not using ViewState and doing a PostBack, make sure you have rebuilt your DropDownList before setting the value.

share|improve this answer
I think my problem lies in that it is existing with a zero value, not that my code was faulty. Apologies for the poor explanation. You answered the question I suppose~ –  Remm Jul 6 '11 at 20:50

A master page in .NET is just a class, right? Let's take a look at it this way. Your page uses that master page. But this means that your page has that master page, not is that master page. Therefore, theoretically you should be able to get a reference to that master page from your page. Let's use other words to make it more clear:

 // This is like the master page
 public class Hand

 // This is like the page. Please notice that a human **has** hands, but a human **is** not hand.
 public class Human
     Hand leftHand = new Handl();

Now, think. Is it possible to get to fingers through an instance of human class? Do you see the logic here? Of course you can get a reference to the Hand class, and from there, you can get reference to fingers:

 Human jack = new Human();

Now what if your hand is a special type of hand (Special master page which has a label in it?). All you need is a cast


That's the logic between a page and a master page.

share|improve this answer

Your Answer


