How do i get master page's Controls value into Content page!!!

  Hi to All,

  I have a Master Page that Contains 4 dropdown lists and 2 Checkboxlists(that contains lot of check box list items) and one Search Button. now i want to search according to master page's controls parameters and display it on content page.now my problem is how do i get the master page's controls(dropdown lists,Checkboxlists)  values into content page.

i did use properties for that in master page and calling these properties in  content page like this:

Master.master:

public string RegionState

{

get

{

if (Session["regionState"] == null)

{

return null;

}

else

{

return Session["regionState"].ToString();

}

}

set

{

Session[
"regionState"] = value;

}

}

ContentPage.aspx

if (Master.RegionState != null)

{

ViewState[
"regionState"] = Master.RegionState;

}

else

{

ViewState[
"regionState"] = "";

}

but i dont want to use Session in master page's properties bcoz if i will search again than it will search with already session value. so please can any body give me anothe idea to over come with this problem.

Thanks

Ravi

 

 

0
mcamail2002
10/2/2007 5:10:58 PM
asp.net.navigation-controls 13714 articles. 0 followers. Follow

16 Replies
2435 Views

Similar Articles

[PageSpeed] 20

There's a way to do it with reflection.

http://www.velocityreviews.com/forums/t122475-how-to-access-the-property-of-master-page.html

 

 


I love to display the non-secure items...
Charlie Asbornsen
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
0
Charles
10/2/2007 7:11:55 PM

Sorry Charles.. my question is how do i will get values of master page controls  for example  suppose i m selcting "City" from dropdownlistCity and "Region" from   dropdownlistRegion  and selecting multiple items from "CheckboxlistServices" then how do i will get thses values in content page from Master Page.

0
mcamail2002
10/2/2007 7:29:58 PM

If you can look at the master page's properties, you can look at the control collection, right?

http://www.codekeep.net/snippets/0451a411-a6f8-4f78-9959-be8e9fa69393.aspx

Call it with

string city =( (DropDownList)FindControlRecursive(this.Master, "dropdownlistCity")).SelectedValue;
That work?
 

I love to display the non-secure items...
Charlie Asbornsen
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
-1
Charles
10/2/2007 7:40:30 PM

Charles.......i did used following property in Master Page:(did not declared "Static")

Master.master:

public Control FindControlRecursive(Control root, string ControlID)

{

if (root.ID == ControlID)

{

return root;

}

foreach(Control c in root.Controls)

{

Control t = FindControlRecursive(c, ControlID);if (t != null)

{

return t;

}

}

return null;

}

ContentPage.aspx: On Page load

string city =( (DropDownList)FindControlRecursive(this.Master, "dropdownlistCity")).SelectedValue;

but i,m getting value of string "city" is blank....whats i,m missing is there???

0
mcamail2002
10/2/2007 9:17:48 PM

 Hay,

 

try to add this row after the page Tag :

<%@ MasterType VirtualPath="~/MasterPage.master" %> , with your master page virtual path.

 

if you write this u will access to your Master page properties directly with :

 

this.Master.PropertyName.

 

good luck,

 

shimon

 

0
shimondahan1
10/2/2007 10:21:01 PM

Already added....this directive<%@ MasterType VirtualPath="~/MasterPage.master" %>

0
mcamail2002
10/3/2007 9:43:51 PM

Hi Ravi,

Base on your description, you want to access the controls which is on the master page in the content page, right?

You can try to use two methods:

1. You can use the findcontrol method in the conteent page to get the control of the master page. 

For example, if you want to acces the dropdownlist control in content page, you can do like below:

 DropDownList ddl = (DropDownList)this.Page.Master.FindControl("ctl00$DropDownList1");
"ctl00$DropDownList1" is the DropDownList1's clientID, you must use the control's clientID if you use the master page, or else, you cannot find the control.
2. You can declare a public property, and set the public property is the DropDownList control object, that you can access the master page's public property in
the content papge. Like below.
  a. Declare a public property and set its value is the DropDownList1 ;
  public DropDownList ddl
    {
        get
        {
            return this.DropDownList1;
        }
    }
 
b. Add the MasterType  in the content page's aspx code.
 
<%@ Page Language="C#" MasterPageFile="~/TreeviewStyle/SelectedStyle/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TreeviewStyle_SelectedStyle_Default" Title="Untitled Page" %>
<%@ MasterType VirtualPath="~/TreeviewStyle/SelectedStyle/MasterPage.master" %>
 
 c. Access the public property in the content page:
 
protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList ddlist = this.Master.ddl;

    }
 

 

Hope it helps.


Amanda Wang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Amanda
10/4/2007 2:26:57 AM

Thanks alot for your Reply...But I wanted to access the control's value which are on the master page in the content page.

Master Page:

DropDownList ddl = (DropDownList) this.Master.Vendor;if (ddl != null)

{

string str1 = ddl.SelectedValue;

Int32 ii= ddl.SelectedIndex;

}

0
mcamail2002
10/4/2007 6:02:07 PM

Hello mcamail2002,

 I have to place the master.propertyname.selectedValue in the PreRenderComplete event on the content page in order to get the current value.

 I hope this helps.

Paul

0
pgrimes
10/4/2007 6:20:37 PM

Hi Paul... but how i will write the PreRenderComplete event... can u give me the code.

0
mcamail2002
10/4/2007 7:00:46 PM

 Hi mcamail2002,

It should be as simple as this. 

protected void Page_PreRenderComplete(object sender, EventArgs e)
    {
        DropDownList ddlist = this.Master.ddl;
        //more code

    }

 

0
pgrimes
10/4/2007 7:12:41 PM

first of all thanks to u.... 

Smile its working... but it always returning

ddl.SelectedIndex's Value ZERO. means it always returning first item of the dropdown list item.

 

0
mcamail2002
10/4/2007 7:49:03 PM

Hello mcamail2002,

There is only one thing I can think of that might cause your problem.  Do you have the autopostback set to true on your drop down list?

Paul

0
pgrimes
10/4/2007 8:12:57 PM

Yes

0
mcamail2002
10/4/2007 8:23:09 PM

OK, I just got latest on a project and it has 12 errors and 43 warnings and I really want to reformat whatever disk SoureSafe is on...

Have you tried any of the following less elegant options:

Create an object with one property, the value you need, and bind the DDL to that.

Create a session variable and update it on the DDL's SelectedIndexedChanged and look it up when you need it.

Create a one row one column table in your data layer and bind the DDL on the master page to that?  Then when you need the data you can go to the data layer.

 


I love to display the non-secure items...
Charlie Asbornsen
Dont forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
0
Charles
10/4/2007 8:47:35 PM

Since you can access the control in the masterpage, why can not access its value? Just like the following example code, I did access the selected value of the dropdownlist which is placed in masterpage. or have I misunderstand you?

protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = ((DropDownList)Master.FindControl("DropDownList1")).SelectedValue;
    }


Johnson
0
Johnson2007
10/8/2007 6:18:02 AM
Reply:

Similar Artilces:

How to set a Master Page's control's property through content page on a postback event?
Hi all, i m using a master page in my application. i have a panel on master page and a tab container with three tab panels on content page. i want to set/change  the visibility of the panel control when the tab is changed on the content page. By default the panel is invisible. To implement this i executed the following steps: 1. Set AutoPostBack of tacontainer to true 2. Wrote an event handler for ActiveTabIndexChanged event of the tab container 3 Wrote the following code on Tab changed event   if (TabContainerEmpPerformance.ActiveTabIndex == 1) { ...

Content controls have to be top-level controls in a content page or a nested master page that references a master page.
Why in the world am I getting this error? The solutions I have seen either don't apply or don't work.  Here is my master page code:<%@ Master Language="C#" AutoEventWireup="false" CodeFile="TechLMSMasters/MainSide.master.cs" Inherits="MainSide" %> <%@ Register TagPrefix="usercontrol" TagName="Search" Src="~/library/uc/Search.ascx" %> <%@ Register TagPrefix="usercontrol" TagName="UserName" Src="~/library/uc/UserNameDisplay.ascx" %><!DOCTYPE html PUBLI...

Problem to use TreeView Control in a 'content page' This Error Occur "Only Content controls are allowed directly in a content page that contains Content controls."
hi I am trying to use the TreeView Control inside the content page (Page that has a master page is a content page). On compiling the project I got the following ASP.Net runtime error: "Only Content controls are allowed directly in a content page that contains Content controls."B@b@R J Issue resolved. I was putting script tag in the content page that is not allowed. when remove the script from content page problem is resolved.B@b@R J...

Error : Content controls have to be top-level controls in a content page or a nested master page that references a master page.
when I make a directory on my root, and in that directory I put a masterpage and a web form ... I get always an error : Content controls have to be top-level controls in a content page or a nested master page that references a master page.   Does somebody know what the problem is ? Is it possible you can't set a masterpage in a directory ... ?Kind Regards,Joeri Pansaerts.From Bruges, Belgium :) i've founded ... I had to nest my masterpage ...Kind Regards,Joeri Pansaerts.From Bruges, Belgium :) Hi Joeri Pansaerts, I am receiving the same error as the one posted her...

ERROR Content controls have to be top-level controls in a content page or a nested master page that references a master page.
This all used to work until about 5 minutes ago. Did I change something? For some reason I am now getting this error: Content controls have to be top-level controls in a content page or a nested master page that references a master page. What I have:  I have a masterpage inside of a directory. That directory is a first level directory with its own web.config file....The whole thing is protected by the web.config file above it (in the main public directory). That main public directory has a masterpage as well. I am not currently linking the two masterpages together becuase...

How could a master page control call a content page's function?
Dear all: For example: 1. In master page, place a button named "button1" . 2. Add three content page, ( add some code )3. Run application, in content page 1, click button1, show "this is page1",  in content page2, click button1, show "this is page2", and so on . Besides, these code should be in every content page instead of in master page, because actually action should be very complex, master can't predict everything. Is this possible?  Thanks!  Here's a great tutorial that may get you started: Creating a Common Toolbar in ASP.NET ...

Accessing a master page's control in a nested content Page(Urgent)
Hi all, I have a masterpage rm_master.masterand another masterpage rm_sub.master which inherits from rm_master.master. Now I want to access a dropdownlist control in a content page which derieves from rm_select_master. the code i used wasDropDownList DropDownList1 = (DropDownList)Master.FindControl("DropDownList1");string a; a = DropDownList1.Text;   The error i got was "Object reference not set to an instance of an object". Please help.. Thanks in advance.. Bharath You can try Page.FindControl or use the mastertype directivePlease remember to click “...

passing querystring from user control to master page's content page
Hi all,        I have a master page which contains two linkbuttons. I want that when user clicks first link button then querystring value is passed to content page's Initialize culture method. how can i do this. English | Spanish < - user control link Master page contains this user control Default page's initializeCulture method need to know which link is clicked and then set culture according.  code inside initializeculute is like thisif (Request.QueryString["Culture"] == null) {Culture = "en-US"; } else {Culture =...

Hook up to a control's event located on the Master Page from the Content Page PLEASE HELP
I have a master page with a button, and I what to hook the button click event on the content page I allready tried this example http://fredrik.nsquared2.com/viewpost.aspx?PostID=328&showfeedback=true but in vb it doesn't work. Thank you ALL. Master Page Code: Public Event OnbtnGrava_Click As EventHandler Protected Sub btnGrava_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGrava.Click RaiseEvent OnbtnGrava_Click(sender, e) End Sub Content Page code: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim mpButton ...

CS0012 when referencing controls on master page's master page.
 I'm working with a content page that has a master page (we'll call it ChildMaster). ChildMaster also has a master page (ParentMaster). There is a control on ParentMaster I need to access from the content page. The code looks like this:  ControlType controlInstance = (ControlType) Master.Master.FindControl("controlID");  When I upload, I get CS0012 telling me that ParentMaster is defined in an assembly that is not referenced, and I need to add a reference to some assembly with a funky random-looking name (which changes when I reupload ParentMaster). Up t...

How can I access the property a control on a master page from the master page's code behind.
On my master page I have a label. I want to change this label's text property based on an element of the querystring. (label.text = "NewValue")All child pages using this master will display the same thing so I was trying to put the code in the master page so as not to have to duplicate it for the child pages or put it in a base class and use FindControl.Is it possible to access from a master page's codebehind the property of a server control that is placed on a master page's aspx? If so how?(I saw similar questions, but they all came from the view of a child accessing the property.)Thank you...

How to acess the master page control value in content page
i want to acess the control which is the part of master page. i want that contol`s  value on the content page.   Friend, try to implement the logic mentioned here http://forums.asp.net/t/1272824.aspx and this one http://wiki.lessthandot.com/index.php/ASP.NET:_Access_Master_Page_controls_from_the_Content_Page Good luck!Please Don't forget to click "Mark as Answer" on the post that helped you.This can be beneficial to other community members reading the thread. Hi, You can try to refer this article:  Passing Information Between Content and Master Pages&...

Value of the control at Master page is not being set from the content page
I am trying to set the value of the control at Master Page from the child page(content page) but the value of the control at master page is not changing.."Hope its your Solution so Mark it as Answer"Ahmad SheikhMicrosoft Valuable Geekhttp://sharpcontents.blogspot.com Here is a good tutorial on doing this from scott. http://www.odetocode.com/Articles/450.aspx here is the part you are looking for I think. Protected Sub Page_Load(ByVal sender As Object, _                     &...

Doing Postback from master page to page in master's content place holder(search page)
Hi, Currently I am trying to do a postback which returns results. The master page has a textfield and button and the button redirects to the search page.The search page also has a textfield and button and when the button is clicked, the query statements take the input from teh user and looks for results. Currently, A Crosspage postback of master page + other pages in content place holder ------> master page+search page in content place holder works.I am trying to do a postback which returns results in master page+search page which gets its input from the textbox of the master page ...

Web resources about - How do i get master page's Controls value into Content page!!! - asp.net.navigation-controls

Resources last updated: 12/1/2015 3:35:32 PM