1

I'm very new to .NET and unfortunately having to take over an ex-colleague's project.

Part of my code has a while loop to loop out query results...

        while (reader.Read())
        {
            pageTitle.Text = reader["title"].ToString();
        }

I am currently using this to retrieve the Page Title from a Table in my Database.

I want to display this in my <title></title> of the SiteMaster file, but unfortunately right now using this method:

<title><asp:Label ID="pageTitle" runat="server" /></title> is giving me a span tag inside the <title>. I want to remove the altogether and just have the Title value.

Can anyone explain the best way to do this?

Appreciate your help and patience with this.

Thank you

1
  • Wouldn't that loop overwrite the previous titles if you have a result set of more than one item? Is this the desired functionality? Commented May 17, 2011 at 15:24

2 Answers 2

3

Make sure the <head> tag has the runat="server" attribute, for example:

<head runat="server">

Then set the title by using:

Page.Title = reader["title"].ToString();
Sign up to request clarification or add additional context in comments.

5 Comments

Here is a link about how to do this: 4guysfromrolla.com/articles/051006-1.aspx
Wow! Many Thanks, Dana!!! And if I were to do this for my meta keywords or meta description, would it be Page.Keywords =.... ?
@user644978: Page.MetaKeywords use VS intellisense once in a while. won't hurt :)
For .NET 4.0 you can use the Page.MetaKeywords. For previous versions you need to declare the tag <meta id="Keywords" name="Keywords" />, which will create an instance of System.Web.UI.HtmlControls.HtmlMeta that you can access from code.
Wow! Tremendous help again, Dana. Page.MetaKeywords and Page.MetaDescription worked mucho coolness. Thanks to @naveen too and @BiggsTRC for the linkage. You guys rock.
3

Use the <asp:Literal> tag instead.

6 Comments

This would be my recommendation. asp:Label renders as span. Literal gives you what you need.
hmmm... why shouldn't he use Page.Title
He could, as dana has suggested. This is an answer to getting rid of the span tag as asked.
Thanks, Druid. I'll look into this tag just now and report back.
@Khepri and @Druid: I totally disagree. framework should be your friend right? whats the point in putting a literal inside the title tag and then calling the literal when you could just use Page.Title
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.