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

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

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

任何人都可以建议一些框架或者他们认为有用的技术来完成这项任务吗? 谢谢。

我使用showmessage jquery插件和页面上的一些扩展方法,如下所示:

///  /// Shows the errors. ///  /// The page. /// The text. public static void ShowError(this Page page, string text) { ShowNotification(page, NotificationType.Error, text, false); } ///  /// Shows the information. ///  /// The page. /// The text. public static void ShowInformation(this Page page, string text) { ShowNotification(page, NotificationType.Information, text, true); } ///  /// Shows the errors. ///  /// The page. /// The text. 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); } } } ///  /// Shows the notifications. ///  /// The page. /// The text. public static void ShowSuccess(this Page page, string text) { ShowNotification(page, NotificationType.Success, text, true); } } 

这不是完美的,但它做我想要的,它很简单,一切都在一个地方。

最简单的方法之一是使用页面方法并让客户端检查页面方法以查看是否有要显示的消息。 然而,这剂量使用肥皂类型的对象调用。 如果你想使用更安全的解决方案,我建议使用wcf get service并执行定时检查或使用回调作为通知客户端的方式。

这两个选项都将提供您需要的数据,但我会首先尝试页面方法,因为它实现起来较短。

到目前为止,最好的方法是将ajax与asp.net mvc3框架和razor结合使用,它实现了一个名为unobstrusive javascript的概念。 有关演练,请参阅http://www.asp.net/mvc/tutorials/creating-a-mvc-3-application-with-razor-and-unobtrusive-javascript 。

我更喜欢有一个隐藏字段,我用我的Javascript检查值。 如果有certian值,那么我会显示我的模态。 我仍然使用标签作为显示器。 当它调用模态弹出窗口时,将隐藏字段值重置为null。

我确信专业人士的答案是做出你要求的最正确和最正确的方式。 但是,如果你像我一样,并没有完全理解答案,请看看我的简单解决方案。

在我加载的类中 – 当然在您的代码中,您可以放置​​这样的子类

 Public Shared Sub messageBox(ByVal SimpleSingleLineString As String) System.Web.HttpContext.Current.Response.Write("alert(""" & SimpleSingleLineString & """);" & vbNewLine) End Sub 

当您需要相对简单地显示来自任何aspx代码的弹出窗口时

  Catch ex As Exception messageBox("We apologise but we could not connect to the help servers at this moment. Please try again in 1 to 5 minutes. Error 100") End Try 

我加载了jQuery。 我不记得是否必须加载但肯定存在JAVASCRIPT支持。

这些是ASP页面,我不加载任何其他类或附加任何其他任何.js文件。

我这样做是因为我习惯于VB应用程序中的messagebox.show(“…”)..

编辑但是,此代码总是在来自另一个页面的每次调用时在服务器端呈现。 像服务一样。 所以调用页面即时获取代码,html,JAVASCRIPT等。它可能需要稍加修改。 调用页面将其作为脚本附加