Master Pages - moving controls in code behind?

  • Thread starter Rob R. Ainscough
  • Start date
R

Rob R. Ainscough

I'm running into a problem with Master Pages.

I have some "absolute" controls (labels, textboxes) that I position on the
page using:

ControlObject.Style.Add("Top", "250")

or

ControlObject.Style.Add("Left", "100")

regardless of what value I use, the controls are always being placed at
0,0 -- any suggestions? This method worked fine when I was not using Master
Pages, but when I converted over to Master Pages this no longer works.

Thanks, Rob.
 
C

clintonG

We can't reference controls directly when used with Master Pages. We have to
reference controls used with Master Pages using a public Property or the
FindControl method. You need to find articles and learn how Master Pages are
actually compiled into the page to really understand whats going on. This is
a must-do. For now...

Its fairly easy to use FindControl when the control is in the .master itself
but when the control is located in a content page and the controls are
templated "finding" a control using FindControl can become frustrating.

I've included a couple of URLs [1,2] to help you get started and my own
little trick which uses trace enabled in the content page to locate the
control when the control is located in templates making it easy to use
FindControl.

// Enable trace in the content page and locate the control named
"CreateUserButton"

// ctl00$InnerPanelContent$CreateUserWizard$__CustomNav2$CreateUserButton

// Use FindControl on each control in the path of the control tree hierarchy
Button createUserButton =
(Button)Page.Master.FindControl("InnerPanelContent").FindControl("CreateUserWizard").FindControl("__CustomNav2").FindControl("CreateUserButton");

// Now we can directly reference the control
createUserButton.Text = "Button Text";

There's got to be better way but I haven't figured it out yet and the
articles I've read don't say much about the context of controls when deeply
nested in templates.

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/


[1] http://msdn2.microsoft.com/en-us/library/xxwa0ff0(vs.80).aspx
[2] http://www.odetocode.com/Articles/450.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,043
Members
48,769
Latest member
Clifft

Latest Threads

Top