Master Pages and accessing the collection of WebControls

S

Steve

I'm trying to iterate over all the form controls in my MasterPages content
page. Basically, I've got dropdowns, textboxes, etc that I want to format
in Page_Load()

I've added this code to my Page_Load() event
<code>
private void ConfigureFormControls()
{
Color borderColor = ColorTranslator.FromHtml("#F5F5F5");
Color fontColor = borderColor;
Color backgroundColor = ColorTranslator.FromHtml("#0D0D0D");

foreach(Control control in this.Controls)
{
if(control is TextBox || control is DropDownList || control is
Button)
{
WebControl webControl = control as WebControl;

webControl.BorderStyle = BorderStyle.Solid;
webControl.BorderWidth = 1;
webControl.BorderColor = borderColor;
webControl.ForeColor = fontColor;
webControl.BackColor = backgroundColor;
}
}
}
</code>

Problem is, the 'this.Controls' collection only has a count of 1, but I have
over 45 textboxes alone! So obviously, I'm hitting the wrong collection,
but as I inspected this.Controls while debuggin I didn't understand how I
was looking at a MasterPage control as the only control in the collection.

I'm sure one of you know exactly what I'm doing wrong, I would really
appreciate a tip.

BTW, I'm not too skilled in the ol' CSS yes, that's why I'm doing it this
way. I can get the dropdowns to work with my CSS, but the textboxes refuse.
Just for kicks, here is my CSS:
..input, select
{
background: #1E1E1E;
color: #F5F5F5;
border: 1px solid #F5F5F5;
}

Thanks for reading,
Steve
 
K

Ken Cox [Microsoft MVP]

Hi Steve,

You can get the parent control using the Parent property. So,

foreach (Control control in TextBox1.Parent.Controls)

would find the common parent of the controls at the same level of Textbox1.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
 
S

Steve

Hi Ken,

My problem is that in my content page, 'this.Controls' is the master page
control.
So I'm confused given a master control, how am I expected to get to a
content pages' controls? This seems especially odd considering I'm in the
Page_Load event of THE content page.

Maybe I'm still missing something?

Thanks for the reply, I appreciate it!

Ken Cox said:
Hi Steve,

You can get the parent control using the Parent property. So,

foreach (Control control in TextBox1.Parent.Controls)

would find the common parent of the controls at the same level of
Textbox1.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

Steve said:
I'm trying to iterate over all the form controls in my MasterPages
content page. Basically, I've got dropdowns, textboxes, etc that I want
to format in Page_Load()

I've added this code to my Page_Load() event
<code>
private void ConfigureFormControls()
{
Color borderColor = ColorTranslator.FromHtml("#F5F5F5");
Color fontColor = borderColor;
Color backgroundColor = ColorTranslator.FromHtml("#0D0D0D");

foreach(Control control in this.Controls)
{
if(control is TextBox || control is DropDownList || control is
Button)
{
WebControl webControl = control as WebControl;

webControl.BorderStyle = BorderStyle.Solid;
webControl.BorderWidth = 1;
webControl.BorderColor = borderColor;
webControl.ForeColor = fontColor;
webControl.BackColor = backgroundColor;
}
}
}
</code>

Problem is, the 'this.Controls' collection only has a count of 1, but I
have over 45 textboxes alone! So obviously, I'm hitting the wrong
collection, but as I inspected this.Controls while debuggin I didn't
understand how I was looking at a MasterPage control as the only control
in the collection.

I'm sure one of you know exactly what I'm doing wrong, I would really
appreciate a tip.

BTW, I'm not too skilled in the ol' CSS yes, that's why I'm doing it this
way. I can get the dropdowns to work with my CSS, but the textboxes
refuse. Just for kicks, here is my CSS:
.input, select
{
background: #1E1E1E;
color: #F5F5F5;
border: 1px solid #F5F5F5;
}

Thanks for reading,
Steve
 
G

Guest

with a master page you need to first find the contentplaceholder(1) then you
can find the controls on a page. I use a lot of dynamicly created controls
1.1 and switching to 2.0/w master pages threw me till i figured it out.
like this
dim thscontrol as [controltype] =
me.masterpage.findcontrol("contententplaceholder").findcontrol("(whet you
swant to find")

or you should be able to loop through when you find the contentplaceholder.
Hope this helps!

--
thanks (as always)
some day i''m gona pay this forum back for all the help i''m getting
kes


Steve said:
Hi Ken,

My problem is that in my content page, 'this.Controls' is the master page
control.
So I'm confused given a master control, how am I expected to get to a
content pages' controls? This seems especially odd considering I'm in the
Page_Load event of THE content page.

Maybe I'm still missing something?

Thanks for the reply, I appreciate it!

Ken Cox said:
Hi Steve,

You can get the parent control using the Parent property. So,

foreach (Control control in TextBox1.Parent.Controls)

would find the common parent of the controls at the same level of
Textbox1.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

Steve said:
I'm trying to iterate over all the form controls in my MasterPages
content page. Basically, I've got dropdowns, textboxes, etc that I want
to format in Page_Load()

I've added this code to my Page_Load() event
<code>
private void ConfigureFormControls()
{
Color borderColor = ColorTranslator.FromHtml("#F5F5F5");
Color fontColor = borderColor;
Color backgroundColor = ColorTranslator.FromHtml("#0D0D0D");

foreach(Control control in this.Controls)
{
if(control is TextBox || control is DropDownList || control is
Button)
{
WebControl webControl = control as WebControl;

webControl.BorderStyle = BorderStyle.Solid;
webControl.BorderWidth = 1;
webControl.BorderColor = borderColor;
webControl.ForeColor = fontColor;
webControl.BackColor = backgroundColor;
}
}
}
</code>

Problem is, the 'this.Controls' collection only has a count of 1, but I
have over 45 textboxes alone! So obviously, I'm hitting the wrong
collection, but as I inspected this.Controls while debuggin I didn't
understand how I was looking at a MasterPage control as the only control
in the collection.

I'm sure one of you know exactly what I'm doing wrong, I would really
appreciate a tip.

BTW, I'm not too skilled in the ol' CSS yes, that's why I'm doing it this
way. I can get the dropdowns to work with my CSS, but the textboxes
refuse. Just for kicks, here is my CSS:
.input, select
{
background: #1E1E1E;
color: #F5F5F5;
border: 1px solid #F5F5F5;
}

Thanks for reading,
Steve
 
G

Guest

i see you are using C# i did a vb reply, but i hope you get the idea.
--
thanks (as always)
some day i''m gona pay this forum back for all the help i''m getting
kes


Steve said:
Hi Ken,

My problem is that in my content page, 'this.Controls' is the master page
control.
So I'm confused given a master control, how am I expected to get to a
content pages' controls? This seems especially odd considering I'm in the
Page_Load event of THE content page.

Maybe I'm still missing something?

Thanks for the reply, I appreciate it!

Ken Cox said:
Hi Steve,

You can get the parent control using the Parent property. So,

foreach (Control control in TextBox1.Parent.Controls)

would find the common parent of the controls at the same level of
Textbox1.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

Steve said:
I'm trying to iterate over all the form controls in my MasterPages
content page. Basically, I've got dropdowns, textboxes, etc that I want
to format in Page_Load()

I've added this code to my Page_Load() event
<code>
private void ConfigureFormControls()
{
Color borderColor = ColorTranslator.FromHtml("#F5F5F5");
Color fontColor = borderColor;
Color backgroundColor = ColorTranslator.FromHtml("#0D0D0D");

foreach(Control control in this.Controls)
{
if(control is TextBox || control is DropDownList || control is
Button)
{
WebControl webControl = control as WebControl;

webControl.BorderStyle = BorderStyle.Solid;
webControl.BorderWidth = 1;
webControl.BorderColor = borderColor;
webControl.ForeColor = fontColor;
webControl.BackColor = backgroundColor;
}
}
}
</code>

Problem is, the 'this.Controls' collection only has a count of 1, but I
have over 45 textboxes alone! So obviously, I'm hitting the wrong
collection, but as I inspected this.Controls while debuggin I didn't
understand how I was looking at a MasterPage control as the only control
in the collection.

I'm sure one of you know exactly what I'm doing wrong, I would really
appreciate a tip.

BTW, I'm not too skilled in the ol' CSS yes, that's why I'm doing it this
way. I can get the dropdowns to work with my CSS, but the textboxes
refuse. Just for kicks, here is my CSS:
.input, select
{
background: #1E1E1E;
color: #F5F5F5;
border: 1px solid #F5F5F5;
}

Thanks for reading,
Steve
 
S

Steve

I do get the idea and I feel a bit lame that it's that simple :0)
Maybe I'm not paying close enough attention, I swore the single control
contained in Page.Controls was a MasterPage and not a MasterPageContent.

Either way, you solution solved my problem and for that...I thank you.

Have a good one.

WebBuilder451 said:
i see you are using C# i did a vb reply, but i hope you get the idea.
--
thanks (as always)
some day i''m gona pay this forum back for all the help i''m getting
kes


Steve said:
Hi Ken,

My problem is that in my content page, 'this.Controls' is the master page
control.
So I'm confused given a master control, how am I expected to get to a
content pages' controls? This seems especially odd considering I'm in
the
Page_Load event of THE content page.

Maybe I'm still missing something?

Thanks for the reply, I appreciate it!

message
Hi Steve,

You can get the parent control using the Parent property. So,

foreach (Control control in TextBox1.Parent.Controls)

would find the common parent of the controls at the same level of
Textbox1.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

I'm trying to iterate over all the form controls in my MasterPages
content page. Basically, I've got dropdowns, textboxes, etc that I
want
to format in Page_Load()

I've added this code to my Page_Load() event
<code>
private void ConfigureFormControls()
{
Color borderColor = ColorTranslator.FromHtml("#F5F5F5");
Color fontColor = borderColor;
Color backgroundColor = ColorTranslator.FromHtml("#0D0D0D");

foreach(Control control in this.Controls)
{
if(control is TextBox || control is DropDownList || control is
Button)
{
WebControl webControl = control as WebControl;

webControl.BorderStyle = BorderStyle.Solid;
webControl.BorderWidth = 1;
webControl.BorderColor = borderColor;
webControl.ForeColor = fontColor;
webControl.BackColor = backgroundColor;
}
}
}
</code>

Problem is, the 'this.Controls' collection only has a count of 1, but
I
have over 45 textboxes alone! So obviously, I'm hitting the wrong
collection, but as I inspected this.Controls while debuggin I didn't
understand how I was looking at a MasterPage control as the only
control
in the collection.

I'm sure one of you know exactly what I'm doing wrong, I would really
appreciate a tip.

BTW, I'm not too skilled in the ol' CSS yes, that's why I'm doing it
this
way. I can get the dropdowns to work with my CSS, but the textboxes
refuse. Just for kicks, here is my CSS:
.input, select
{
background: #1E1E1E;
color: #F5F5F5;
border: 1px solid #F5F5F5;
}

Thanks for reading,
Steve
 
G

Guest

I know the feeling way too well! I'm just glad I was finally able to help
some one around here!

--
thanks (as always)
some day i''m gona pay this forum back for all the help i''m getting
kes


Steve said:
I do get the idea and I feel a bit lame that it's that simple :0)
Maybe I'm not paying close enough attention, I swore the single control
contained in Page.Controls was a MasterPage and not a MasterPageContent.

Either way, you solution solved my problem and for that...I thank you.

Have a good one.

WebBuilder451 said:
i see you are using C# i did a vb reply, but i hope you get the idea.
--
thanks (as always)
some day i''m gona pay this forum back for all the help i''m getting
kes


Steve said:
Hi Ken,

My problem is that in my content page, 'this.Controls' is the master page
control.
So I'm confused given a master control, how am I expected to get to a
content pages' controls? This seems especially odd considering I'm in
the
Page_Load event of THE content page.

Maybe I'm still missing something?

Thanks for the reply, I appreciate it!

message
Hi Steve,

You can get the parent control using the Parent property. So,

foreach (Control control in TextBox1.Parent.Controls)

would find the common parent of the controls at the same level of
Textbox1.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

I'm trying to iterate over all the form controls in my MasterPages
content page. Basically, I've got dropdowns, textboxes, etc that I
want
to format in Page_Load()

I've added this code to my Page_Load() event
<code>
private void ConfigureFormControls()
{
Color borderColor = ColorTranslator.FromHtml("#F5F5F5");
Color fontColor = borderColor;
Color backgroundColor = ColorTranslator.FromHtml("#0D0D0D");

foreach(Control control in this.Controls)
{
if(control is TextBox || control is DropDownList || control is
Button)
{
WebControl webControl = control as WebControl;

webControl.BorderStyle = BorderStyle.Solid;
webControl.BorderWidth = 1;
webControl.BorderColor = borderColor;
webControl.ForeColor = fontColor;
webControl.BackColor = backgroundColor;
}
}
}
</code>

Problem is, the 'this.Controls' collection only has a count of 1, but
I
have over 45 textboxes alone! So obviously, I'm hitting the wrong
collection, but as I inspected this.Controls while debuggin I didn't
understand how I was looking at a MasterPage control as the only
control
in the collection.

I'm sure one of you know exactly what I'm doing wrong, I would really
appreciate a tip.

BTW, I'm not too skilled in the ol' CSS yes, that's why I'm doing it
this
way. I can get the dropdowns to work with my CSS, but the textboxes
refuse. Just for kicks, here is my CSS:
.input, select
{
background: #1E1E1E;
color: #F5F5F5;
border: 1px solid #F5F5F5;
}

Thanks for reading,
Steve
 

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
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top