Set DropDownList selected value on master page from child page
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; } }
--> Object reference not set to an instance of an object.
Using these normally cause the same error:
t开发者_开发技巧his.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
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:
Master.change_ddlArea(someValue);
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.
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();
jack.Hand.Fingers["SmallFinger"].Cut();
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
((ExpensiveHand)jack.Hand).Rings.Steal();
That's the logic between a page and a master page.
精彩评论