添加toastr javascript asp.net webform

我试图在使用按钮提交表单后显示一个toastr消息(信息,错误等)并更新gridview控件(在asp.net webform中的更新面板中。感谢

您可以使用Page.ClientScript.RegisterStartupScript方法执行此操作。 例:

 Page.ClientScript.RegisterStartupScript(this.GetType(), "toastr_message", "toastr.error('There was an error', 'Error')", true); 

但我可能会创建一个方法或扩展方法来为我处理:

 public static void ShowToastr(this Page page, string message, string title, string type = "info") { page.ClientScript.RegisterStartupScript(page.GetType(), "toastr_message", String.Format("toastr.{0}('{1}', '{2}');", type.ToLower(), message, title), addScriptTags: true); } 

使用:

 ShowToastr(this.Page, "Hello world!", "Hello"); 

如果你想要一些更强大的东西,你可以使type参数成为enum

从网络表单调用,(注意这是一个MasterDetail表单,因此有一个MasterPage。

MasterPage.ShowToastr(页面,“此处留言”,“此处标题”,“信息”,错误,“吐司底部全宽”,假)

Master Page(VB)中的VB.NET ShowToastr实现

 Public Shared Sub ShowToastr(ByVal page As Page, ByVal message As String, ByVal title As String, Optional ByVal type As String = "info", Optional ByVal clearToast As Boolean = False, Optional ByVal pos As String = "toast-top-left", Optional ByVal Sticky As Boolean = False) Dim toastrScript As String = [String].Format("Notify('{0}','{1}','{2}', '{3}', '{4}', '{5}');", message, title, type, clearToast, pos, Sticky) page.ClientScript.RegisterStartupScript(page.[GetType](), "toastr_message", toastrScript, addScriptTags:=True) End Sub 

Javascript函数ShowToastr作为共享函数驻留在母版页中。

        

我希望这可以帮助某些人,因为我多年来一直尝试将toastr选项集成在一个调用中。 如果要在调用toastr时有更多可用选项,请向函数添加更多参数。 可以设置的所有选项都在注释代码(javascript)中。