0

C#コードに(Webフォームを使用して)IF / ELSE条件があり、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 に答える