Body.Attributes.Add

M

mazdotnet

Hi,

I have a master file with the following setting
<body runat="server" id="Body">
and in the .cs I have
Body.Attributes.Add("onload",
"menuCurrentPage('current','navlist');");

Now in one of my content files I want to add an additional parameter
to the onload
HtmlControl body = (HtmlControl)Master.FindControl("Body");
body.Attributes.Add("onload", "alert('hello world');");

However, this overwrites the value in my onload attribute on the
master page and so I lose 'menuCurrentPage('current','navlist');"); '.
How can I append from my content page to an attribute in my master
page? so that the end result would be
<body onload='menuCurrentPage('current','navlist');"); alert('hello
world');");'>


Thanks
M.
 
S

Steve

Try this:

Body.Attributes("onload") += "alert('hello world');"


Steve C.
MCAD,MCSE,MCP+I,CNE,CNA,CCNA
 
M

mazdotnet

Try this:

Body.Attributes("onload") += "alert('hello world');"

Steve C.
MCAD,MCSE,MCP+I,CNE,CNA,CCNA








- Show quoted text -


I tried it and it says
body.Attributes("onload") += "alert('hello world');";
System.Web.UI.HtmlControls.HtmlControl.Attributes' is a 'property' but
is used like a 'method'
Any idea?

Thx
 
A

AreKc

mazdotnet:
I tried it and it says
body.Attributes("onload") += "alert('hello world');";
System.Web.UI.HtmlControls.HtmlControl.Attributes' is a 'property' but
is used like a 'method'
Any idea?

body.Attributes["onload"] += "alert('hello world');";
 
M

mazdotnet

mazdotnet:
I tried it and it says
body.Attributes("onload") += "alert('hello world');";
System.Web.UI.HtmlControls.HtmlControl.Attributes' is a 'property' but
is used like a 'method'
Any idea?

body.Attributes["onload"] += "alert('hello world');";

Still no go. In my content page I have

HtmlControl body = (HtmlControl)Master.FindControl("Body");
body.Attributes["onload"] += "alert('hello world');";

However, on the generated page it still doesn't come up

<body id="ctl00_Body" onload="menuCurrentPage('current','navlist');">

No alert('hello world')

Anything else I could be missing

Thx
M
 
M

Mark Rae [MVP]

HtmlControl body = (HtmlControl)Master.FindControl("Body");
body.Attributes["onload"] += "alert('hello world');";

((HtmlGenericControl)Master.FindControl("Body")).Attributes["onload"] +=
"alert('hello world');";
 
C

chakde dotnet

Hi

ASP.NET would process the content page ahead of the master page. So the
code on your master page would always overwrite your onload event.

Make the following changes in your master page as below:

HtmlControl body = (HtmlControl)Master.FindControl("Body");
body.Attributes["onload"] += "menuCurrentPage('current','navlist');";

Make the following changes in your content page as below:

HtmlControl body = (HtmlControl)Master.FindControl("Body");
body.Attributes["onload"] += "alert('hello world');"

Your content page function will fire before your master page function
fires. Make the following changes to your master page if you want to
change the order of execution.

HtmlControl body = (HtmlControl)Master.FindControl("Body");
body.Attributes["onload"] = "menuCurrentPage('current','navlist');" +
body.Attributes["onload"];

Thanks,
Chakde
 

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

Top