不使用方法的行脚本代码

Script code of line not working with a method

我的 .aspx 页面中有一个脚本(HTML 标记):

<div id="alert">
    <asp:Label ID="lblAlert" style="font-size: xx-large" runat="server"></asp:Label>
</div>
<!-- /.alert -->

<script>

    function AutoHideAlert(v) {
        var al = document.getElementById('<%=lblAlert.ClientID%>');

        al.innerText = v;

        $("#alert").fadeTo(3500, 500).slideUp(1500, function () {
            $("#alert").slideUp(500);
        });
    }

</script>

我正在使用 RegisterStartupScriptaspx.cs 文件(代码隐藏)中调用 AutoHideAlert(v) 函数并且我添加了 RegisterStartupScriptShowMessage 方法中:

private void ShowMessage(string msg)
{
    ScriptManager.RegisterStartupScript(this, GetType(), null, "AutoHideAlert('"+msg+"');", true);
}

问题是当我调用包含脚本代码行的 ShowMessage 方法时它不起作用。但是当我 运行 脚本代码行然后它的工作;问题是为什么不是 运行 ShowMessage?

编辑 1: 从@M4N 的评论中,我尝试将第三个参数设置为 "Alert Message" 但它仍然不起作用。

private void ShowMessage(string msg)
{
    ScriptManager.RegisterStartupScript(this, GetType(), "Alert Message", "AutoHideAlert('"+msg+"');", true);
}

我想 ScriptManager.RegisterStartupScript 在生成带有方法的脚本之前生成脚本。由于 JS 在浏览器读取它时执行,因此在执行时 AutoHideAlert 如果还不存在 尝试使用 $(document).ready 你有 JQuery

ScriptManager.RegisterStartupScript(this, GetType(), "Alert Message",
     "$(document).ready(function(){ AutoHideAlert('"+msg+"'); }", true);

document.addEventListener('DOMContentLoaded'没有JQuery

ScriptManager.RegisterStartupScript(this, GetType(), "Alert Message",
     "document.addEventListener('DOMContentLoaded', 
                                function(){ AutoHideAlert('"+msg+"'); })", true);