Adding toastr javascript asp.net webform

前端 未结 2 856
甜味超标
甜味超标 2021-02-13 18:06

I am trying to display a toastr message (info,error etc) after submitting a form using a button and update a gridview control (which is in a update panel\" in asp.net webform. T

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-13 18:32

    You can do it using Page.ClientScript.RegisterStartupScript method. Example:

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

    But I would probably create a method or extension method to handle that for me:

    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);
    }
    

    Use:

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

    If you want something a little more robust, you could make the type parameter an enum.

提交回复
热议问题