2

I want to show a Bootstrap show modal in the catch exception

Bankend language: C# Bootstrap v3.3.7

protected void Page_Load(object sender, EventArgs e)
{
  try 
  {
    if (!IsPostBack) 
    {

    }
  }
  catch (System.Exception ex)
  {
    label.Tex = ex.toString()<
    //show modal here
  }
}

thank you for you time and help.

Jeroen Heier
  • 3,520
  • 15
  • 31
  • 32
Macdroopy
  • 45
  • 4
  • look here for WebForms and bootstrap modal: https://stackoverflow.com/questions/25172311/display-bootstrap-modal-from-asp-net-webforms –  Jul 10 '18 at 17:46

1 Answers1

0

You can use the ScriptManager

ScriptManager.RegisterStartupScript(this, GetType(),
             "SuccessfullSave",
             @"$('#SuccessfullSave').modal('show'); 
               $('.modal-backdrop').appendTo('#aspnetForm');",
             true);

Let me explain this code:

The ScriptManager.RegisterStartupScript is a method that allows you to inject into your web page javascript

The parameters you pass are:

  1. the page (this)
  2. the type [use GetType()]
  3. the "name" or key of the script
  4. the actual JavaScript itself (which in this case is the JQuery code to show a modal $('#SuccessfullSave').modal('show'); where #successfullSave is the id of the modal and $('.modal-backdrop').appendTo('#aspnetForm'); is code to correct styles so the modal is up in front and not weirdly placed behind a gray see through background)
  5. and the last parameter is a bool value saying insert this code inside of script tags

This completed code would look something like below

protected void Page_Load(object sender, EventArgs e)
{
   try 
   {
      if (!IsPostBack) 
      {

      }
    }
    catch (System.Exception ex)
    {
        label.Text = ex.toString();
        //HERE IS WHERE YOU PUT THIS
        ScriptManager.RegisterStartupScript(this, GetType(),
             "ErrorMessage",
             @"$('#NameOfModal').modal('show'); 
               $('.modal-backdrop').appendTo('#aspnetForm');",
             true);
    }
}

To close the modal: Instead of using the traditional modal dismiss that comes with bootstrap you need to call the opposite of $('#NameOfModal').modal('show'); That is $('#NameOfModal').modal('hide'); or you can just click outside the modal

M Goward
  • 308
  • 1
  • 12
  • it works!! but I can not close it, when a press Close or "x" button in the show modal, how can I close it? – Macdroopy Jul 10 '18 at 19:15