F
Frank
Hi,
I simply wish to code a JS function ONCE, in a .js file, include the .js
file in the Master page, and then in several but not all pages, I wish to
execute the function on pageload.
I don't want to use the
Page.ClientScript.RegisterStartupScript(this.GetType(),"displaymessage",
strScript, false);
method because this seemingly requires me to code the JScript on all the
pages (code behind) where I want to run the function, which is stupid.
I think what I want to use is something like :
Designate the body tag as :
<body runat="server" id="body">
and in the code behind for a particular page:
protected void Page_Load(object sender, EventArgs e)
{
try
{
string temp = "displaymessage();";
HtmlGenericControl body =
(HtmlGenericControl)Master.FindControl("Body");
body.Attributes.Add("onload", temp);
}
catch (NullReferenceException x)
{
Response.Write(x);
}
}
but this give a JS error where it says 'Object Required' where the body tag
is in the rendered html
It does work if I do:
protected void Page_Load(object sender, EventArgs e)
{
try
{
string temp = "alert('Hello World!');";
HtmlGenericControl body =
(HtmlGenericControl)Master.FindControl("Body");
body.Attributes.Add("onload", temp);
}
catch (NullReferenceException x)
{
Response.Write(x);
}
}
So what is wrong?
I simply wish to code a JS function ONCE, in a .js file, include the .js
file in the Master page, and then in several but not all pages, I wish to
execute the function on pageload.
I don't want to use the
Page.ClientScript.RegisterStartupScript(this.GetType(),"displaymessage",
strScript, false);
method because this seemingly requires me to code the JScript on all the
pages (code behind) where I want to run the function, which is stupid.
I think what I want to use is something like :
Designate the body tag as :
<body runat="server" id="body">
and in the code behind for a particular page:
protected void Page_Load(object sender, EventArgs e)
{
try
{
string temp = "displaymessage();";
HtmlGenericControl body =
(HtmlGenericControl)Master.FindControl("Body");
body.Attributes.Add("onload", temp);
}
catch (NullReferenceException x)
{
Response.Write(x);
}
}
but this give a JS error where it says 'Object Required' where the body tag
is in the rendered html
It does work if I do:
protected void Page_Load(object sender, EventArgs e)
{
try
{
string temp = "alert('Hello World!');";
HtmlGenericControl body =
(HtmlGenericControl)Master.FindControl("Body");
body.Attributes.Add("onload", temp);
}
catch (NullReferenceException x)
{
Response.Write(x);
}
}
So what is wrong?