Click here to Skip to main content
15,882,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi When i am working on page without using master page.
I find control by simply
C#
this.page.findControl("txtcontrol")


but when i integrate master page and other user control to that page and try to find control with same. i am getting null..

help me on this.

one more thing when i am see this.page.conrols.count in quick view at a time of debug count always "1".
where as in previous case without masterpage count will as number of page controls.

give me any idea about this

Thanks
Posted

You're only seeing "1" control because you're searching te Master Page, not its Content.

To find a control on a page that has a master, you have to do something more like this:

this.Master.FindControl("MainContentPlaceHolder").FindControl("MyControl")


Adjusting the IDs as necessary, of course.

Controls also have a unique ClientID that looks like this:

ctl00_MainContentPlaceHolder_MyControl


Do a View Source in your browser to see what your controls are actually being named at runtime. You could do a FindControl using that ClientID, but it's cleaner to do it the first way.
 
Share this answer
 
Comments
fjdiewornncalwe 19-Sep-11 10:27am    
It is bad practice for a master page to "know" that much about the children in it. A properly designed UI will have the UI elements of children isolated. If a value from the "txtcontrol" in the child is required by the master page, then the user control should expose the value of txtcontrol as a property that the master page can then read. Even then, the master page should really not know anything about the child controls at all.
GenJerDan 20-Sep-11 11:23am    
Isolated how? Where would you put the the controls so they aren't wrapped in the master? Don't you put them in the Content areas?
AditSheth 21-Sep-11 0:05am    
Thanks Gen
this.Master.FindControl("MainContentPlaceHolder").FindControl("MyControl")
I implemented it and its really works fine
my vote 5
You need to search for the control within the code behind for the user control, not the master page. The master page should never need to know anything about internals of the user controls that are being shown from the master page and the user controls should know nothing of the master page other than the public placeholder names. If you need to do this, then I would suggest that the inherent design of your pages is incorrect and that the solution to your problem is a correction of this design issue as opposed to trying to do things in the wrong place.
 
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