简体   繁体   中英

Bootstrap modal popup opens and closes immediately

I have a bootstrap popup displaying student results but it opens and closes immediately. my master page file has following included js files

<html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
   <title></title>
     <asp:ContentPlaceHolder ID="head" runat="server">
     </asp:ContentPlaceHolder>
   <script src="jquery-1.12.3.min.js" type="text/javascript"></script>
   <script src="js/bootstrap.js" type="text/javascript"></script>
   <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" />  
</head>

and my webpage has no file included for bootstrap only a jquery timer file

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<link href="css/jquery.countdownTimer.css" rel="stylesheet" type="text/css" />
</asp:Content>

The code for modal is as below

<div class="modal fade" id="myModalTest" role="dialog">
<div class="modal-dialog">

  <!-- Modal content-->
  <div class="modal-content">
    <div class="modal-header">
     <%-- <button type="button" class="close" data-dismiss="modal">&times;</button>--%>
      <h4 class="modal-title text-center">RESULT</h4>
    </div>
    <div class="modal-body">
       <table class="table table-bordered" style="background-color:White" cellpadding="10" cellspacing="10">
    <tr>
        <td class="style1">
            <p class="text-justify"><asp:Label runat="server" ID="Label1" Text="Test Name :"></asp:Label>
        </td>
        <td>
            <asp:Label runat="server" ID="lbltest"></asp:Label>
        </td>
    </tr>
    <tr>
        <td class="style1">
            <asp:Label runat="server" ID="lblName" Text="Student Name :"></asp:Label>
        </td>
        <td>
            <asp:Label runat="server" ID="lbluname"></asp:Label>
        </td>
     </tr>
     <tr>
        <td class="style1">
            <asp:Label runat="server" ID="lblmks" Text="Total marks scored :"></asp:Label>
        </td>
        <td>
            <asp:Label runat="server" ID="lblmarksscored"></asp:Label>
        </td>
    </tr>
    <tr>
        <td class="style1">
            <asp:Label runat="server" ID="Label2" Text="Total questions attempted :"></asp:Label>
        </td>
        <td>
            <asp:Label runat="server" ID="lbltot"></asp:Label>
        </td>
    </tr>
    <tr>
        <td class="style1">
            <asp:Label runat="server" ID="Label3" Text="Total questions in Test :"></asp:Label>
        </td>
        <td>
            <asp:Label runat="server" ID="lbltotalTestQ"></asp:Label>
        </td>
    </tr>
 </table>
    </div>
    <div class="modal-footer">
      <asp:Button class="btn btn-default" runat="server" ID="btnBackModal" Text="back" OnClick="btnBackModal_Click"/>
     </div>
  </div>

</div>
</div>

Even the back button's ie btnBackModal OnClick event does not fire. When I debugged the code the control does not go to that event on the server side.

 public void btnBackModal_Click(object sender, EventArgs e)
    {
        if (isSaved == 0)
            saveAnswer();
        Response.Redirect("StudentHome.aspx");
    }

I solved the problem by writing javascript function to open modal instead of using data-toggle attribute for button. I removed the code

<button input="button" class="btn btn-info" id="btnshowmodal" data-toggle="modal" data-target="#myModalTest">View Result</button>

and instead wrote a javascript function to handle onclick event.

HTML:

<button input="button" class="btn btn-info" id="btnshowmodal" onclick="popup();return false;" runat="server">View Result</button>

JS:

function popup() {
  $('[id*="myModalTest"]').modal('show');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM