I had previously created a sample that displayed all of the controls on
a page but I failed to look closely at what was really there.  For some
reason I thought that the title would not show up in the controls
collection but that didn't make sense.  So after pulling my head out I
pumped out a piece of code that will do what I want.  It is pretty much
what everyone has said but I have actually tested it.  It is not bullet
proof but does the job.  It should be reworked with regular expressions
to take into account all of the ways the tags could be typed in.


private void Page_Load(object sender, System.EventArgs e)
        {
                string myTitle = GetTitle(this.Page.Controls);
                Response.Write(myTitle);

        }

        private string GetTitle (ControlCollection controlNode)
        {
                foreach (Control control in controlNode)
                {
                        if (control as System.Web.UI.LiteralControl !=
null )
                        {
                        if(
((System.Web.UI.LiteralControl)control).Text.ToUpper().IndexOf("<TITLE")
> -1)
                        {
                                //parse out the title.
                                string literal =
((System.Web.UI.LiteralControl)control).Text.ToUpper();
                        int startTitleOpenPos =
literal.IndexOf("<TITLE");
                        int endTitleOpenPos =
literal.IndexOf(">",startTitleOpenPos);
                        int endTitleClosePos =
literal.IndexOf("</TITLE>");
                        return literal.Substring(endTitleOpenPos +
1,endTitleClosePos);
                        }
                        }
                }
                return null;
        }

Remember that if you set the runat attribute to server then the title
tags are no longer a part of the literal but rather a
HTMLGenericControl.  The text bellow was considered one literal.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
        <HEAD>
                <title>CP-ShowControls</title>
                <meta content="Microsoft Visual Studio 7.0"
name="GENERATOR">
                <meta content="C#" name="CODE_LANGUAGE">
                <meta content="JavaScript"
name="vs_defaultClientScript">
                <meta
content="http://schemas.microsoft.com/intellisense/ie5";
name="vs_targetSchema">
                <LINK href="/Cp_Web/Css/cpCommonStyles.css"
type="text/css" rel="stylesheet">
                <LINK href="/Cp_Web/Css/cpSideBar.css" type="text/css"
rel="stylesheet">
        </HEAD>
        <body MS_POSITIONING="GridLayout">

-----Original Message-----
From: dotnet discussion [mailto:[EMAIL PROTECTED]] On Behalf Of
Kirk Jackson
Sent: Thursday, May 02, 2002 3:34 PM
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET] Extracting the Title of a Page from CodeBehind?

You could iterate through the controls of the page, looking for a
LiteralControl that contains the <title> and </title> strings - but
that's probably not the nicest way to do things!

E.g.

foreach (System.Web.UI.Control c in this.Controls)
{
        if (c is System.Web.UI.LiteralControl)
        {
                // Look inside ((System.Web.UI.LiteralControl)c).Text
                Response.Write( ((System.Web.UI.LiteralControl)c).Text);
        }
}

Kirk

-----Original Message-----
From: Joseph E Shook [mailto:[EMAIL PROTECTED]]
Sent: Friday, 3 May 2002 8:27 a.m.
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET] Extracting the Title of a Page from CodeBehind?


That is a nice solution.  It works.  But Visual Studio removes the runat
attribute when switching between HTML and Design view.

Also I would still like to extract the title from codebehind without
having to add attributes.  I could just put a constant in each page but
this has turned into a personal exercise in technology.

-----Original Message-----
From: dotnet discussion [mailto:[EMAIL PROTECTED]] On Behalf Of
Patten, Dave
Sent: Thursday, May 02, 2002 12:59 PM
To: [EMAIL PROTECTED]
Subject: Re: [DOTNET] Extracting the Title of a Page from CodeBehind?

Html:

<title id="PageTitle" runat="server">Page Title</title>

Code Behind:

protected HtmlGenericControl PageTitle;
private void Page_Load(object sender, System.EventArgs e)
{
        // get or set
        PageTitle.InnerText = "New Title";
}


-----Original Message-----
From: Joseph E Shook [mailto:[EMAIL PROTECTED]]
Sent: Thursday, May 02, 2002 12:55 PM
To: [EMAIL PROTECTED]
Subject: [DOTNET] Extracting the Title of a Page from CodeBehind?


Does anyone have a clever way of extracting the title of a page in the
codebehind?



I am talking about the tile that is resident in the following html tags:



<HTML>

<HEAD>

            <title>Page Title</title>

</HEAD>



<body>



</body>

</HTML>

You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET,
or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to