【问题标题】:show modal Bootstrap on catch exception在捕获异常时显示模态引导程序
【发布时间】:2018-07-10 17:25:08
【问题描述】:

我想在 catch 异常中显示一个 Bootstrap 显示模式

银行端语言:C# 引导程序 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
  }
}

感谢您的时间和帮助。

【问题讨论】:

标签: c# modal-dialog try-catch


【解决方案1】:

您可以使用ScriptManager

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

让我解释一下这段代码:

ScriptManager.RegisterStartupScript 是一种允许您将 javascript 注入网页的方法

你传递的参数是:

  1. 页面(this
  2. 类型[使用GetType()]
  3. 脚本的“名称”或键
  4. 实际的 JavaScript 本身(在本例中是 JQuery 代码 显示模态 $('#SuccessfullSave').modal('show'); 其中 #successfullSave 是模态的 ID,$('.modal-backdrop').appendTo('#aspnetForm'); 是要更正的代码 样式,所以模态在前面,而不是奇怪地放在后面 灰色透视背景)
  5. 最后一个参数是bool 值,表示插入此代码 脚本标签内

这个完整的代码如下所示

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

关闭模式: 您需要调用 $('#NameOfModal').modal('show'); 的反面,即 $('#NameOfModal').modal('hide');,而不是使用引导程序附带的传统模态关闭,或者您可以在模态外部单击

【讨论】:

  • 它有效!但是我无法关闭它,当在显示模式中按关闭或“x”按钮时,我该如何关闭它?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-12
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 2012-10-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多