0

我的 C# 代码中有一个 IF/ELSE 条件(使用 webforms),我想使用 ClientScript.RegisterClientScriptBlock 来更改锚标记类

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

我在其他地方的项目中使用了客户端脚本,但它在这里不起作用。

附加信息:

此代码位于页面加载时调用的方法中。不知道这是否有区别。

4

1 回答 1

0

我改变了这个:

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
    {
        EmailAction = "delete";
        lblEmailText.Text = "Remove from Emails";
        //Change Anchor class to show remove image
        ClientScript.RegisterStartupScript(typeof(string), "ChangeAnchorClass", "<script>document.getElementById('hrefEmail').className = 'MyClass';</script>");

    }
于 2013-04-09T09:27:06.143 回答