繁体   English   中英

为什么我不能在类的updatepanel页面中显示js错误警报?

[英]Why I can't display an js error alert in updatepanel page from class?

我有一个单选按钮(在updatepanel中),并且在checkchanged事件中,我正在向用户显示一条消息。 但是div位于母版页中,而页面未呈现,这就是为什么我无法显示消息的原因。 有可能解决这个问题吗? 顺便说一句,我正在使用类来显示消息。 这是我的代码。

public static void setError(System.Web.UI.Page p, string title, string error, string type)
    {
        Literal literal = p.Master.FindControl("ltMessage") as Literal;

        if (type == "error")
        {
            literal.Text = "<div id=\"alertError\" style=\"display:none; color:#fff; text-align:center; padding: 8px 10px 9px; width:auto; position:relative; background:#cc0000;\"><h3>" + title + "</h3><p>" + error + "</p></div>";
            //literal.Text = "<div id=\"alert\" class=\"error message\"><h3>" + title + "</h3><p>" + error + "</p></div>";

            StringBuilder sb = new StringBuilder();
            sb.Append("$(function() { ");
            sb.Append(" $('#alertError').toggle({");
            sb.Append("    width: 400");
            sb.Append(" });");
            sb.Append("});");

            if (HttpContext.Current.CurrentHandler is Page)
            {
                Page pa = (Page)HttpContext.Current.CurrentHandler;

                if (ScriptManager.GetCurrent(pa) != null)
                {
                    ScriptManager.RegisterStartupScript(pa, typeof(Page), "alert", sb.ToString(), true);
                }
                else
                {
                    pa.ClientScript.RegisterClientScriptBlock(typeof(Page), "alert", sb.ToString(), true);
                }
            }
        }
}

不确定您如何调用该setError。 但是$(function() {...})$(document).ready(function(){...})的同义词,仅在页面加载后才调用。 UpadtePanel正在执行AJAX调用,因此不会运行$(document).ready事件。 请尝试以下代码:

StringBuilder sb = new StringBuilder();
sb.Append(" $('#alertError').toggle({");
sb.Append("    width: 400");
sb.Append(" });");

如果您正确执行了其他所有操作,则此方法应该起作用。

我修好了它。 我将div放到了母版页的updatepanel中。 现在,它可以完美运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM