0
我在我的C#代码(使用webforms)中有IF/ELSE条件,我想使用ClientScript.RegisterClientScriptBlock更改锚标签类ClientScript.RegisterClientScriptBlock - 无法设置属性'className'的值:对象为空或未定义
email = Business.Core.Profiles.Email.SetEmailAlertProfile(base.companyId, type);
// Email obj == null means no alert data for advertiser with this ID.
if (email == null)
{
EmailAction = "add";
lblEmailText.Text = "Add to Email Alerts";
//Change Anchor class to show remove image
//ClientScript.RegisterClientScriptBlock(typeof(string), "ChangeAnchorClass", "<script>$('.jsPopUp btnAddEList active').addClass('jsPopUp btnRemoveEList active');</script>");
}
else
{
EmailAction = "delete";
lblEmailText.Text = "Remove from Emails";
//Change Anchor class to show remove image
ClientScript.RegisterClientScriptBlock(typeof(string), "ChangeAnchorClass", "<script>document.getElementById('hrefEmail').className = 'MyClass';</script>");
}
我ELSE条件使我有以下错误:
Microsoft JScript runtime error: Unable to set value of the property 'className': object is null or undefined
我已经使用客户端脚本在我的项目在其他地方,但它不是在这里工作。
附加信息:
此代码位于页面加载时调用的方法中。不知道这是否有所作为。
这是不是DOM运行前准备,因此它不能找到元素,或者它不会有一个页面上运行一个具有'hrefEmail'的'id'的元素。 – 2013-03-08 15:04:13
@AnthonyGrist好吧,如果是这样的话,哪个事件在asp.net web形式的生命周期中,我会触发这个代码? – 2013-03-08 15:06:01
我不是一个ASP.net开发人员,所以我的知识主要局限于JavaScript代码的客户端运作,但[此问题](http://stackoverflow.com/questions/666519/difference-between- registerstartupscript-and-registerclientscriptblock)应该涵盖你所需要的。看起来你需要使用'RegisterStartupScript'而不是'RegisterClientScriptBlock'。 – 2013-03-08 15:08:52