使用 jQuery 从 ASP.NET 向用户显示消息

2024-03-25

在 ASP.NET 中开发各种 Web 应用程序时,我发现自己需要在执行各种操作后将消息发送回用户。例如,文件是否已成功上传或数据库记录已更新。另外,如果有错误我想通知用户。

到目前为止,我一直在创建包含要向用户显示的消息的服务器端变量,然后使用最初隐藏但随后在回发时可见的 ASP.NET Label 控件来显示消息。这很有效,但我真的很喜欢在模态 jQuery 窗口中显示一些消息的选项,以便我可以确保他们看到该消息。

任何人都可以建议一些他们发现对完成此任务有用的框架或技术吗?谢谢。


我用显示消息 http://showmessage.dingobytes.com/download/jquery插件结合页面上的一些扩展方法,如下所示:

    /// <summary>
    /// Shows the errors.
    /// </summary>
    /// <param name="page">The page.</param>
    /// <param name="text">The text.</param>
    public static void ShowError(this Page page, string text)
    {
        ShowNotification(page, NotificationType.Error, text, false);
    }

    /// <summary>
    /// Shows the information.
    /// </summary>
    /// <param name="page">The page.</param>
    /// <param name="text">The text.</param>
    public static void ShowInformation(this Page page, string text)
    {
        ShowNotification(page, NotificationType.Information, text, true);
    }

    /// <summary>
    /// Shows the errors.
    /// </summary>
    /// <param name="page">The page.</param>
    /// <param name="text">The text.</param>
    public static void ShowNotification(this Page page, NotificationType notificationType, string text, bool autoClose)
    {
        string className = null;
        switch (notificationType)
        {
            case NotificationType.Error:
                className = "fail";
                break;
            case NotificationType.Information:
                className = "notification";
                break;
            case NotificationType.Success:
                className = "success";
                break;
        }

        string notification = "jQuery('body').showMessage({'thisMessage':['" + text.Replace(Environment.NewLine, "','") + "'],'className':'" + className + "','autoClose':" + autoClose.ToString().ToLower() + ",'delayTime':4000,'displayNavigation':" + (!autoClose).ToString().ToLower() + ",'useEsc':" + (!autoClose).ToString().ToLower() + "});";

        if (RadAjaxManager.GetCurrent(page) != null)
        {
            RadAjaxManager.GetCurrent(page).ResponseScripts.Add(notification);
        }
        else
        {
            if (ScriptManager.GetCurrent(page) != null)
            {
                ScriptManager.RegisterStartupScript(page, page.GetType(),
                                                    "notification",
                                                    notification,
                                                    true);
            }
            else
            {
                page.ClientScript.RegisterStartupScript(page.GetType(),
                                                        "notification",
                                                        notification,
                                                        true);
            }
        }
    }

    /// <summary>
    /// Shows the notifications.
    /// </summary>
    /// <param name="page">The page.</param>
    /// <param name="text">The text.</param>
    public static void ShowSuccess(this Page page, string text)
    {
        ShowNotification(page, NotificationType.Success, text, true);
    }
}

它并不完美,但它满足了我的要求,它很简单,一切都集中在一处。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 jQuery 从 ASP.NET 向用户显示消息 的相关文章