G
Guest
Hello All--
I'm having a problem getting an onclick event to work. Here is my setup: I
have an external JavaScript file, a master page that registers the javascript
as an external file, and a content page that has controls that I am trying to
get onclick to work for.
// =====================
// MyScriptFile.js
// =====================
function ReportOnClick(msg)
{
alert('ReportOnClick: ' + msg);
}
// =====================
// MasterPage.master.cs
// =====================
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptInclude("MyJavaScriptFile",
"MyScriptFile.js");
}
// =====================
// ContentPage.aspx.cs
// =====================
protected void Page_Load(object sender, EventArgs e)
{
if (! IsPostback)
{
// Does not work
Label1.Attributes.Add("onclick", "ReportOnClick('Label1 was
clicked.');");
// Does not work
// Label1.Attributes.Add("onclick",
"javascript:ReportOnClick('Label1 was clicked.');");
// Does work
// Label1.Attributes.Add("onclick", "alert('Label1 was clicked.');");
}
}
So, the adding of the attribute works fine, as I get an alert if I hardcode
one in. However, whenever I try to access a function in my external script,
I get a "Microsoft JScript runtime error: Object expected".
Any ideas?
Thanks,
PAGates
I'm having a problem getting an onclick event to work. Here is my setup: I
have an external JavaScript file, a master page that registers the javascript
as an external file, and a content page that has controls that I am trying to
get onclick to work for.
// =====================
// MyScriptFile.js
// =====================
function ReportOnClick(msg)
{
alert('ReportOnClick: ' + msg);
}
// =====================
// MasterPage.master.cs
// =====================
protected void Page_Load(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptInclude("MyJavaScriptFile",
"MyScriptFile.js");
}
// =====================
// ContentPage.aspx.cs
// =====================
protected void Page_Load(object sender, EventArgs e)
{
if (! IsPostback)
{
// Does not work
Label1.Attributes.Add("onclick", "ReportOnClick('Label1 was
clicked.');");
// Does not work
// Label1.Attributes.Add("onclick",
"javascript:ReportOnClick('Label1 was clicked.');");
// Does work
// Label1.Attributes.Add("onclick", "alert('Label1 was clicked.');");
}
}
So, the adding of the attribute works fine, as I get an alert if I hardcode
one in. However, whenever I try to access a function in my external script,
I get a "Microsoft JScript runtime error: Object expected".
Any ideas?
Thanks,
PAGates