PDA

View Full Version : Database queries in title



CurtWRC
04-19-2006, 01:35 PM
How can I display text in the <title> of an .aspx page by retreiving it from a database using a querystring to determine which record. For example, on this forum when you view a thread, the title of that thread appears as the title of the page.

Any help would be appreciated. Thanks.

Brandoe85
04-19-2006, 05:26 PM
Assuming you can do the query portion, just check the querystring value and make sure it's not null. If it's not, pass it to a function that grabs the title from the db and you're done :)

Some method in your codebehind:


protected string getTitle(int id)
{
// do some db query to grab this

return "this is a title for " + id.ToString();
}

Then in your html:


<title>
<% Response.Write((Request.QueryString.Get("id") != null) ? getTitle(Convert.ToInt32(Request.QueryString.Get("id"))) : "default title"); %>
</title>


Good luck;

CurtWRC
04-20-2006, 01:17 AM
Thanks for the help. I did it a bit of an easier way in the end, but thanks for putting me in the right direction.

Brandoe85
04-20-2006, 05:03 AM
Thanks for the help. I did it a bit of an easier way in the end, but thanks for putting me in the right direction.
Don't be shy ;)

CurtWRC
04-20-2006, 04:06 PM
Well the way I did it before was by just putting:

<title>
Query_DB(Select ...)
</title>

But that just displayed it as text. However by putting:

<title>
<% Response.Write(Query_DB(Select ...)) %>
</title>

Then it worked.