Click here to Skip to main content
15,899,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

I have MasterPage1 and MasterPage2 that its master page is MasterPage1

and ASP.net page called MyPage its master page is MasterPage2 like that:

MasterPage1 >>>>> MasterPage2 >>>>>>>>>> MyPage

I want to access controls found in MasterPage2 from MyPage
I can access the controls in the outer MasterPage, but the problem i cannot access the controls in the inner MasterPage, Although I thought it easy

I try that but not worked with me:

C#
LinkButton tb = this.Master.FindControl("lbtnContact") as LinkButton;
Posted
Comments
_Amy 11-Aug-12 23:43pm    
Are you using nested master?
MrLonely_2 11-Aug-12 23:49pm    
yes
_Amy 12-Aug-12 0:09am    
try my answer.

I think you should use following code:
C#
ContentPlaceHolder cp = this.Master.Master.FindControl("MainContent") as   ContentPlaceHolder;
LinkButton lb = cp.FindControl("lbtnContact") as LinkButton;


where your main(topmost) masterpage has a contentplaceholder like:
ASP.NET
<asp:contentplaceholder id="MainContent" runat="server" />


I hope it will help you.
 
Share this answer
 
v2
Comments
MrLonely_2 12-Aug-12 1:13am    
Thanks, but i tried it before also and not work
Raje_ 12-Aug-12 3:30am    
I think you are not properly nesting your master page.
Refer this link:
http://www.asp.net/web-forms/tutorials/master-pages/nested-master-pages-cs
Go to "Step 2: Creating a Simple Nested Master Page" this section.
MrLonely_2 13-Aug-12 0:44am    
Sorry Rajesh Biswas, your answer is right and it worked nice with me
Thank you very much
Raje_ 13-Aug-12 0:49am    
Welcome.
Try this code:
C#
LinkButton tb = (LinkButton)Master.FindControl("lbtnContact")

Good Luck
 
Share this answer
 
Comments
MrLonely_2 11-Aug-12 23:37pm    
I tried it before but not worked
Refer this[^] link for the similar discussion. This will help you.
And please read Nested ASP.NET Master Pages[^], it'll help you to understand the flow.



--Amit
 
Share this answer
 
v2
Comments
MrLonely_2 12-Aug-12 0:11am    
I tried it before, that can access the controls in first or outer MasterPage only, i want controls in inner MasterPage which is the master page for the page that i write code on it
_Amy 12-Aug-12 0:25am    
Try my updated answer.
MrLonely_2 12-Aug-12 1:14am    
Thanks Amy, i will read and tell you
Hi,

if you know that you have fix master page hierarchy then you can easily find the control like ,
C#
(this.Page.Master.Master.FindControl("textbox1") as TextBox).Text 

In above example my parent master page contain one text box with textbox1 as it's ID and i would like to find the Text property of that text box.

Hope from this code you will get idea about how you can get your control information.

Thanks
-Amit Gajjar.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900