where exactly in the page should i put it ?
Main Topics
Browse All TopicsWe have a web site built in C# / ASP.NET
Currently we have one title that applies for all pages so if you navigate to each of the pages of
http://www.m-photo.com you can verify that the title remains the same.
We want a script that will change the title dynamically so if for example I navigate to : http://www.m-photo.com/pro
And I press on each of the products located in the left menu I will get a page with a different title according to the ID
For example
http://www.m-photo.com/pro
http://www.m-photo.com/pro
Etc&
This is the code of our Portal. Master page (the main template)
<%@ Master Language="C#" AutoEventWireup="true" %>
<%@ Import namespace="System" %>
<%@ Import namespace="System.Text" %>
<%@ Import namespace="System.Configur
<%@ Import namespace="System.Collecti
<%@ Import namespace="System.Web" %>
<%@ Import namespace="System.Web.Secu
<%@ Import namespace="System.Web.UI" %>
<%@ Import namespace="System.Web.UI.W
<%@ Import namespace="System.Web.UI.W
<%@ Import namespace="System.Web.UI.H
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.Sql
<%@ Register src="~/App_Controls/suppor
<%@ Register src="~/App_Controls/TopMen
<%@ Register src="~/App_Controls/News.a
<%@ Register src="~/App_Controls/footer
<%@ Register src="~/App_Controls/copyri
<script runat="server">
//Constants defining title for "unnamed" page and
const string DEFAULT_UNNAMED_PAGE_TITLE
const string DEFAULT_PAGE_TITLE = "M-Photo Ltd. - Color correction tools for professional photolabs";
protected void Page_Load(object sender, System.EventArgs e)
{
//Set the page's title, if needed
if (string.IsNullOrEmpty(Page
{
Page.Title = DEFAULT_PAGE_TITLE;
}
}
</script>
This is the code of one of the default.aspx (in this case it is the http://www.m-photo.com/pro
<%@ Page Language="C#" MasterPageFile="~/App_Mast
<%@ Register Src="~/App_Controls/front.
<asp:Content ID="Content1" runat="server" contentplaceholderid="midP
<asp:Repeater id="Repeater1" runat="server" DataSourceID="dsGeneral">
<ItemTemplate>
<h2><%# DataBinder.Eval(Container.
<%# DataBinder.Eval(Container.
</ItemTemplate>
</asp:Repeater><asp:SqlDat
<SelectParameters>
<asp:QueryStringParameter Name="ProdID" QueryStringField="ProdID"
Type="Int32" DefaultValue="1" />
<asp:Parameter DefaultValue="2" Name="ProdCatID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>
<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="left
<uc1:front ID="front1" runat="server" />
</asp:Content>
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
In Portal.Master i have something similar to what you've mentioned :
<script runat="server">
//Constants defining title for "unnamed" page and
const string DEFAULT_UNNAMED_PAGE_TITLE
const string DEFAULT_PAGE_TITLE = "M-Photo Ltd. - Color correction tools for professional photolabs";
protected void Page_Load(object sender, System.EventArgs e)
{
//Set the page's title, if needed
if (string.IsNullOrEmpty(Page
{
Page.Title = DEFAULT_PAGE_TITLE;
}
}
</script>
So should i put it there ? however i still not understand how by using the following line :
"this.Title = "My New Title";
i can give a different title to each page
If you set the page title in the Master page it basically sets the name for all the pages that use the master page. So if you want to have individual names for each of your pages you must set their names in their own Page_Loads. Thus if you do not specify a name change in the page load of an individual page, it gets the name change from the master page, ie. DEFAULT_PAGE_TITLE. But if you do put some kind of name change in the 'child' pages using the master page, this will override the name which you specified.
I just used "My New Title" as an example and this.Title will give you the same result as Page.Title. You can give it your individual names.
To view the Page_Load of the other pages double click somewhere on the form when you are in design view.
I have attached a screenshot on how to view the code of a aspx page. Remember, view the code of the child pages, not the master page.
In your case I see you use querystrings to pass the product ID from one page to the next.
So in the orinigating page you encode the values, something like so:
string ProdID = Server.UrlEncode(txtProdID
Response.Redirect("Page2.a
And to decode it at the destination page, something like this:
string ProdID = Request.QueryString("ProdI
Response.Write("Your Product ID is " + ProdID);
The decoding part will usually be in the Page_Load. Then it is up to you specify the title of the page.
Something like this (in the page load):
string ProdID = Request.QueryString("ProdI
Page.Title = "Your Product ID is " + ProdID;
This is just an example.
Hope it helps.
The relevant default.aspx and the ascx files attached below.
Can you specify exactly where should i locate the snippets you mentioned above ?
default.aspx
<%@ Page Language="C#" MasterPageFile="~/App_Mast
<%@ Register Src="~/App_Controls/front.
<asp:Content ID="Content1" runat="server" contentplaceholderid="midP
<asp:Repeater id="Repeater1" runat="server" DataSourceID="dsGeneral">
<ItemTemplate>
<h2><%# DataBinder.Eval(Container.
<%# DataBinder.Eval(Container.
</ItemTemplate>
</asp:Repeater><asp:SqlDat
<SelectParameters>
<asp:QueryStringParameter Name="ProdID" QueryStringField="ProdID"
Type="Int32" DefaultValue="1" />
<asp:Parameter DefaultValue="2" Name="ProdCatID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>
<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="left
<uc1:front ID="front1" runat="server" />
</asp:Content>
front.ascx
<%@ Control Language="C#" %>
<%@ Register Assembly="RadMenu.Net2, Version=4.4.2.0, Culture=neutral, PublicKeyToken=bbe59a8ad35
Namespace="Telerik.WebCont
<%@ Import namespace="System" %>
<%@ Import namespace="System.Text" %>
<%@ Import namespace="System.Configur
<%@ Import namespace="System.Collecti
<%@ Import namespace="System.Web" %>
<%@ Import namespace="System.Web.Secu
<%@ Import namespace="System.Web.UI" %>
<%@ Import namespace="System.Web.UI.W
<%@ Import namespace="System.Web.UI.W
<%@ Import namespace="System.Web.UI.H
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.Sql
<table style="border-style: solid; border-width: thin; border-top-color: 788F97; border-right-color: 788F97; border-bottom-color: 788F97; border-left-color: 788F97;"
width="193px"><tr><td><asp
<tr><td>
<radM:RadMenu ID="RadMenu1" runat="server" DataSourceID="SqlDataSourc
Skin="Vista" Width="190px"
DataFieldID="ProdID"
>
<DataBindings>
<radM:RadMenuItemBinding TextField="title" NavigateUrlField="NavURL" />
<radM:RadMenuItemBinding TextField="ProdID" Depth="1" />
</DataBindings>
</radM:RadMenu>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:portalDS
SelectCommand="SELECT [ProdID] ,[ProdCatID], [title], [display_order], [full_text], [lang_id] ,'~/products/front/default
<SelectParameters>
<asp:Parameter DefaultValue="en" Name="lang_id" Type="String" />
<asp:Parameter DefaultValue="2" Name="ProdCatID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</td></tr>
</table>
Ok, so now we generated cs file and as you can see down below we inserted a code related to
the decoding part.Is this the right place to insert it ? now, where should i insert the encoding part ?
In the default.aspx itself ?
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.
using System.Web.UI.HtmlControls
namespace WebApplication1
{
public partial class default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
{
string ProdID = Request.QueryString("ProdI
Page.Title = "Your Product ID is " + ProdID;
}
}
}
}
The decoding part is fine inside the Page_Load like below (I just took out the extra brackets). The encoding part you put in the code where you redirect to the new URL. Such as a button on the originating page. For example, if the current page is http://localhost/default.a
If your default page (where you click to go to the product page) has a .cs file, I would create a button and put it in the code of the button click.
Just to make sure if i did it right.
Please go to http://www.m-photo.com/pro
In the left side of the page you can see a dynamic menu (which function like buttons). If you stand on one of the item , you can see down below the full address with the PRODID after the question mark.
Now, please see down below the encoding part that i integrated in the default.aspx file
(I put this snippet within the <SelectParameters> tags )
<%@ Page Language="C#" MasterPageFile="~/App_Mast
<%@ Register Src="~/App_Controls/Backen
<asp:Content ID="Content1" runat="server" contentplaceholderid="midP
<asp:Repeater id="Repeater1" runat="server" DataSourceID="dsGeneral">
<ItemTemplate>
<h2><%# DataBinder.Eval(Container.
<%# DataBinder.Eval(Container.
</ItemTemplate>
</asp:Repeater><asp:SqlDat
<SelectParameters>
<asp:QueryStringParameter Name="ProdID" QueryStringField="ProdID"
Type="Int32" DefaultValue="0" />
string ProdID = Server.UrlEncode(txtProdID
Response.Redirect("default
<asp:Parameter DefaultValue="1" Name="ProdCatID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Content>
<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="left
<uc1:Backend ID="Backend1" runat="server" />
</asp:Content>
Unfortunately i get a crash
Server Error in '/' Application.
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="mycustomp
</system.web>
</configuration>
Do you have access to the server running the application? If you do you can run it there and see what the error is. If not, can you possible run it on your local machine and see if you get the same error. I went to the URL you gave and it worked fine for me. When do you get the error? When you redirect to a destination page, or what were you doing when it occured?
Well , i'm not an ASP.NET specialist but when i look at the two lines i added to default.aspx i can see immediately that something in the syntax is wrong.
You can verify it yourself : the two lines -
string ProdID = Server.UrlEncode(txtProdID
Response.Redirect("default
appear after the closing tag which is : /> .
Now , as i can see in the file every command should be within starting tag and closing tag like :
<asp:Parameter ..... /> and these lines are NOT.
I'm 100% sure that this is the cause for the error and it can't be run as is. Something in the syntax should be changed.
Please look at it once again and notify me how to write it properly.
I agree with your statement that this is the cuase for the error. I would put that code inside the button click event of the page wich will be responsible for the redirection. If this is not possible for some reason (the page does not have a code behind .cs file), then you can also possibly use JavaScript to do the redirection. But you cannot place that c# code as it is there in the page source. That code like it is there, needs to be in the code behind part (.cs file) like shown in the code snippet.
Well, to tell you the truth i'm a bit confused. What do you mean by "the code behind .cs file ?"
I attached below the code from the ascx file(this file is being referred from default.aspx) maybe we can insert here the additional lines instead of
using java script ?
<%@ Control Language="C#" %>
<%@ Register Assembly="RadMenu.Net2, Version=4.4.2.0, Culture=neutral, PublicKeyToken=bbe59a8ad35
Namespace="Telerik.WebCont
<%@ Import namespace="System" %>
<%@ Import namespace="System.Text" %>
<%@ Import namespace="System.Configur
<%@ Import namespace="System.Collecti
<%@ Import namespace="System.Web" %>
<%@ Import namespace="System.Web.Secu
<%@ Import namespace="System.Web.UI" %>
<%@ Import namespace="System.Web.UI.W
<%@ Import namespace="System.Web.UI.W
<%@ Import namespace="System.Web.UI.H
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.Sql
<table style="border-style: solid; border-width: thin; border-top-color: 788F97; border-right-color: 788F97; border-bottom-color: 788F97; border-left-color: 788F97;"
width="193px"><tr><td><asp
<tr><td>
<radM:RadMenu ID="RadMenu1" runat="server" DataSourceID="SqlDataSourc
Skin="Vista" Width="190px"
DataFieldID="ProdID"
>
<DataBindings>
<radM:RadMenuItemBinding TextField="title" NavigateUrlField="NavURL" />
<radM:RadMenuItemBinding TextField="ProdID" Depth="1" />
</DataBindings>
</radM:RadMenu>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:portalDS
SelectCommand="SELECT [ProdID] ,[ProdCatID], [title], [display_order], [full_text], [lang_id] ,'~/products/front/default
<SelectParameters>
<asp:Parameter DefaultValue="en" Name="lang_id" Type="String" />
<asp:Parameter DefaultValue="2" Name="ProdCatID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</td></tr>
</table>
I have attached an image of two screenshots. The first view is the source code of an ascx file (or User Web Control), as well as the design view. The second view at the botton is of the code behind file for the same ascx file. All I did was double click the button on my design view and it created the button click event and I placed the code there.
Notice that there is no code inside the ascx source except the code for the user interface controls.
Your example is very good and clear but is not relevant to my situation.
As you saw in our site , we dont use such a button but dynamic menus.
Moreover, redirection works ok and I dont want to touch it. I want only to change the title according ProdID.
As I informed you in one of our earlier correspondence, I already implemented the change of titles in all of the default.aspx where
There is only one page. So I added to each page something like :
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
this.Title = "Our company";
}
</script>
And this works fine.
Now I want to change the script in the following manner so if for example prodid is one then the title will be something like : blaa.. blaa.
I wonder if its not possible to write it in the following manner as written below.
The whole issue with redirection is not relevant for me because its already works only the dynamic titles are missing
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
if (ProdID=1) Page.Title = = product number 1
{
if (ProdID=2) Page.Title = = product number 2
}
{
else Page.Title = product number 3;
}
}
</script>
I tried each of this option and get a crash , should i define the ProdID ?
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="mycustomp
</system.web>
</configuration>
Business Accounts
Answer for Membership
by: dweppenaarPosted on 2008-05-21 at 09:17:00ID: 21616572
So if I understand you correctly you want to change the tile of the page you are currently at, basically overriding the default title you get from the master page. Why not put the name change in the Page Load of the pages implementing the Master Page?