Adding toastr javascript asp.net webform

David Blay picture David Blay · Mar 21, 2013 · Viewed 13.4k times · Source

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. Thanks

Answer

Meryovi picture Meryovi · Mar 21, 2013

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.