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

标签 jquery asp.net

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

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

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

最佳答案

我使用showmessage 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);
    }
}

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

关于jquery - 使用 jQuery 从 ASP.NET 向用户显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5791487/

相关文章:

javascript - 如何创建正确的 PHP json 响应

jquery - 删除某个元素之后的所有元素

javascript - 使用按钮单击表单内部的 Ajax 调用不起作用

javascript - 选择任何选项后如何更改选择菜单背景颜色|不是选项颜色

c# - 匹配时抓取一部分文本,去掉其余部分,因为它没用

javascript - 想要在浏览器窗口最小化或最大化时重置 RadGrid 高度

jquery - 在 IE 10 中成功执行 XMLHttpRequest 后,无法立即通过 .Ajax 发布到新的操作方法

c# - 如果我在代码中设置一些值,我仍然可以使用 UpdateModel() 吗?

c# - 从 AJAX 工具包选项卡面板中删除边框

c# - ContentType.Xml 不存在?