Ensure a control is in the page

D

David

Hi

using C#, .NET 1.1

I want to ensure a particular control is in a certain place in the page...

For example, in the head section, I want to ensure that a literal control of
a certain name is built into the page. This is so that I can add meta
information without having to rely on the content managers to insert it.

How can I ensure that the control is there? Something like...

Page.FindControl("head").FindControl("MetaLiteral") ???

Thanks.

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
M

Michael Nemtsev

Hello David,

Yep, exactly
All page hierarchy is in Page object and it's only way to get it from there

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

D> Hi
D>
D> using C#, .NET 1.1
D>
D> I want to ensure a particular control is in a certain place in the
D> page...
D>
D> For example, in the head section, I want to ensure that a literal
D> control of a certain name is built into the page. This is so that I
D> can add meta information without having to rely on the content
D> managers to insert it.
D>
D> How can I ensure that the control is there? Something like...
D>
D> Page.FindControl("head").FindControl("MetaLiteral") ???
D>
D> Thanks.
D>
 
M

Michael Nemtsev

Hello David,

D> Do I need to ensure the head tag has runat="server" or will it work
D> without it?

Nope, runat has no relation to the contol location

D> All I want to do is to ensure that the literal is in the head area somewhere.

So, I'd recomment to keep the path to this contol somewhere in the config,
and then implement service method, checking that hierary is right


---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
 
D

David

Hhmm... didn't work. :-(

If I have something like...

<head>
<asp:Literal id=MyLiteral runat=server />
</head>

then have in my code...
try
{
System.Web.UI.WebControls.Literal MyLit =
(System.Web.UI.WebControls.Literal)Page.FindControl("head").FindControl("MyLiteral");
MyLit.Text = "<meta name=\"David\" content=\"RevilloC Developments
Ltd. - http://www.revilloc.com\">";
}
catch
{
Response.Write("David - <a href=\"http://www.revilloc.com\">RevilloC
Developments Ltd.</a>");
}

my response.write runs.

However, if I remove the FindControl("head") it works, but that is not what
I want.

If I change the head tag to
<head id=head runat=server>

then it works, but I would prefer not to set up the head like that.

Basically, what I want to achieve is to ensure that a literal is in the head
and that I can target it. If it isn't in the head, then I want to write onto
the page.

Apart from what I have laid out here, what other options do I have?

I don't know what you mean by this. Can you expand?

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
David said:
Thank you. I will give it a go.

Reason I asked about runat in the findcontrol("head")...

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available


Michael Nemtsev said:
Hello David,

D> Do I need to ensure the head tag has runat="server" or will it work
D> without it?

Nope, runat has no relation to the contol location

D> All I want to do is to ensure that the literal is in the head area
somewhere.

So, I'd recomment to keep the path to this contol somewhere in the
config, and then implement service method, checking that hierary is right


---
WBR, Michael Nemtsev [.NET/C# MVP]. My blog:
http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and
we miss it, but that it is too low and we reach it" (c) Michelangelo
 
M

Michael Nemtsev

Hello David,

D> Basically, what I want to achieve is to ensure that a literal is in
D> the head and that I can target it. If it isn't in the head, then I
D> want to write onto the page.
D>
D> Apart from what I have laid out here, what other options do I have?
D>


Hmmm, well.... if you don't use runat=server attribute then your contol will
be flattened in control tree as Literal Control
so, you can iterate through Page.Control, to check whether your "MyLitera"
control locates above your "form1" control, in general your MyLiteral should
locate at Page.Control[1]. Check it
Page.Control[0] is your header without runat=server attribute

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangel
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top