修改脚本管理器的警报消息

时间:2014-04-09 17:07:29

标签: javascript asp.net

有什么方法可以修改脚本管理器的"alert('error !')部分,在下面的标签中包含消息?我不想为此使用标签,只是在弹出窗口中显示数据库错误。我尝试使用+添加它,但要么它不起作用,要么根本不显示弹出窗口。感谢

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('error !')  ", true);

lblError.Text = "A database error has occured. <br /> <br />" +
          "Message: " + ex.Message;

它不喜欢这样:

<!-- all the links for datetime picker -->
    <link rel="stylesheet" type="text/css" href="../style/jquery.datetimepicker.css"/ >
    <script src="../Scripts/jquery.js"></script>
    <script src="../Scripts/jquery.datetimepicker.js"></script>



     <script>
         $(function() {
             $( "#datetimepicker" ).datetimepicker();

         });

     </script>

4 个答案:

答案 0 :(得分:0)

您可以创建一个javascript函数并调用它来更改所需标签的文本。 我的建议是:

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", " changeLabel('" + ex.Message + "', '" + lblError.ClientID +"'); alert('error !');   ", true);

和javascript函数:

function changeLabel(text, id){
    $("#"+id).val(text);
}

答案 1 :(得分:0)

很遗憾,您无法在javascript警告消息中添加HTML标记。

简单警报

确保在 ex.Message 中删除单引号'

try
{
    throw new Exception("test");
}
catch (Exception ex)
{
    string message = string.Format("alert('A database error has occured. {0}');", 
       ex.Message.Replace("'", ""));
    ScriptManager.RegisterStartupScript(this, this.GetType(), 
        "alert" + UniqueID, message, true);
}

jQuery对话

但是,如果要插入HTML,则可以使用jQuery Dialog

enter image description here

<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<div id="dialog" title="Basic dialog">
    <div id="dialog-text"></div>
</div>

try
{
    throw new Exception("This is an error message.");
}
catch (Exception ex)
{
    string message = string.Concat("$(function () {$('#dialog').dialog(); " +
        "$('#dialog-text').html('A database error has occured. " +
        "<br /> <br />", ex.Message, "');});");
    ScriptManager.RegisterStartupScript(this, this.GetType(),
        "alert" + UniqueID, message, true);
}

答案 2 :(得分:0)

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "<script language='javascript'>alert('A database error has occured.\n\n"+ ex.Message +"' );</script>", true);

答案 3 :(得分:0)

try
{

..

}
catch(Exception ex)
{

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Database error ocurred : "+ ex.Message.ToString()+"')", true);

}