I'm looking for a way to access my page's controls.
I found this function that allows me to enter the ID of my control and the "root control"
and than it searches the children of the "root control" untill it finds my control id.
But the problem is that my control is nested in a table and the table is nested in a page in my master page.
whats the root control for the master page?
You can use the Master property of your content page to get the master page, then
FindControl to drill down till you get the control you are looking for (or you can use the function you already have).
See this howto on MSDN.
Try
(this.Page.MasterPage as MyMasterPage).SomeMasterPageMethodOrProperty
Related
I have a button on a Master page and when it is clicked it calls a method on the master page which requires several parameters. The values for the parameters are stored in text boxes and drop down lists on both the master page and the content page. I can get the values from the controls on the master page, but I am not sure how to retrieve the values from the controls on the content page.
I have spent the last day looking at various answers on here and on other sites, but have nor found a solution that works for me.
I have also seen answers that suggest putting the buttons on the control page, but that would mean duplicating code on at least 12 pages and I really dont want to have to go down that route unless I have to.
I forgot to say that I am already using the Master reference tag in the content page.
EDIT
If I create a class and set values in the content page, what would be the best way to retrieve the values in the master page. Assuming that would be possible?
I suggest you add a public method like SetYourDropdownValue(string val) to your master page.
In the load-eventhandler from the control page you hand over the selected value from the dropdown to your master page.
MyMainPage m = (MyMainPage)Master;
m.SetYourDropdownValue("your value");
There is an alternative way tho: Master Pages Reference.
You can set following reference tag in your control page markup:
<%# MasterType virtualPath="~/MasterPage.master"%>
Then it will be even easier:
Master.SetYourDropdownValue("your value");
In the SetYourDropdownValue-Method you save the value to a variable.
On the click eventhandler in your master page, you can then retreive the value from your variable.
Instead of passing a string you could also pass one or more controls, so you would just have to save the reference to it, instead of saving a value.
I am trying to access the master page control id into the content page.
tried with findcontrol and its working, but not understanding the below case.
In master page I have 100 hyperlinks are there, based on click of one of the hyperlink I would like to display the content in content page.
if I know control name then can go for findcontrol, but in this case I don't know which link has been clicked.
any body could you share any resolution criteria.
I am using two Different master page in my site. On one master page i am using a LoginStatus control. So wanna access that LoginStatus control from other. I have no great knowledge about how to find the controls from one master page to other.
Is there any way to achieving this. Any help is deeply appreciated.
Thanks.
Well if there is single master page and content page you can access control using
Label lblinMaster = (Label)This.Master.FindControl("Label1");
and the same way to access control in nested master page you can use following.
Label lblinParentMaster = (Label)Master.Master.FindControl("Label1");
Hope this helps.
Unless you are using nested master pages, you cannot access one master page from another.
A Page would normally use only one master page at a time.
The users login status, however is not dependent on the LoginStatus control.
You can show this status everywhere.
Refer: ASP.NET Login Controls to get an idea about the right control to use.
I have a UserControl that consists of a Parent and Child UserControls that is displayed on a aspx page. I need to get the instance of the Parent UserControls from the child controls. The Parent has a set of nested .net controls and in these nested controls the child UserControls are displayed so if I use this from a child UserControls
MyControl _myControl = (MyControl)this.Parent.Parent.Parent.Parent.FindControl("MyControl");
where (this) = the child control and (Parent.Parent.Parent.Parent) walks me back the tree to the real parent.
This will get me there but there just seems to be a better way. Any suggestions?
An ascx shouldn't know anything about its parent(s): that's a sign that it's too closely coupled to other classes. They might as well be one class.
One alternative is to follow the law of Demeter: figure out what this (your user control) needs from MyControl, make it a property, and let your aspx provide it rather than asking for it.
If you're using a master page, you can start from there and use the container id to find the control. It just depends what the control is closer to.
This might help:
http://www.asp.net/master-pages/tutorials/control-id-naming-in-content-pages-cs
Another thing you can do if you're accessing a value from a control in a parent page, is to put that value into the HttpContext.Current.Items["MyControlValue"] on page load. That way your usercontrol can grab that value easily
So I have my main page with buttons on top of it.
Below there's a MultiView (also tried with a Panel) and I want to display a different page (aspx) inside it when a user clicks the button on top of the page.
I've been trying to do this for 2 hours now and can't get this to work... No good info # Google.
Thanks in advance...
Try user controls instead of pages. I am not aware of any case where I ever needed this functionality.
Beside that, I'm rather sure that it is not supported to insert pages into other pages.