asp.net从Code Behind调用javascript

时间:2013-01-28 06:10:39

标签: c# javascript asp.net html notify

我在html输入上使用NOTIFY来通知我的用户该记录是否已保存。它工作得很好。但是,当我尝试从我的代码隐藏文件中使用它时,它不是。我知道javascript是一种客户端技术并尝试使用RegisterStartupScript但没有运气。

我试图在按钮点击时使用它

        protected void Button1_Click1(object sender, EventArgs e)
    {
        var script = " $.notify.success('I do not want to close by myself close me ', { close: true });";
        ClientScript.RegisterStartupScript(typeof(Page), "ButtonAlert", script, true);

    }

但没有运气。

我确信一旦数据库更新,必须有一种方法在顶部显示通知栏。我们可以使用函数来做吗? 我的脚本定义如下

 <!-- Notify Implementation -->
<script src="../Scripts/jquery-1.9.0.js" type="text/javascript"></script>
<link href="../Styles/notify.css" rel="stylesheet" type="text/css" />
<script src="../Scripts/notify.js" type="text/javascript" ></script>

<script type="text/javascript">

     function myNotify() {
         $.notify.success('I do not want to close by myself close me ', { close: true });
     };

请有人帮忙吗

3 个答案:

答案 0 :(得分:1)

尝试这样: -

  System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("<script language='javascript'>");
                sb.Append("function notify(){");
                sb.Append("$.notify.success('I do not want to close by myself close me ', { close: true });");
    sb.Append("}");
                sb.Append("/script>");

 ClientScript.RegisterStartupScript(typeof(Page), "ButtonAlert", sb, true);

答案 1 :(得分:0)

试试这个:

Page.ClientScript.RegisterStartupScript(this.GetType(),"ButtonAlert","myNotify()",true);

答案 2 :(得分:0)

这解决了HERE中描述的问题。我使用了一个辅助类,并且它解决了这个问题。

    using System.Web.UI;

public static class NotificationHelper
{
    /// <summary>
    /// Shows the successful notification.
    /// </summary>
    /// <param name="page">The page.</param>
    /// <param name="message">The message.</param>
    public static void ShowSuccessfulNotification(this Page page, string message)
    {
        page.ClientScript.RegisterStartupScript(page.GetType(), "notificationScript",
                                                "<script type='text/javascript'>  $(document).ready(function () {  $.notify.success('I do not want to close by myself close me ', { close: true });});</script>");
    }
}