Bootstrap Modal fires but not ASP Button Click eve

2019-05-23 20:39发布

问题:

Basically just like the title says.

Layout: Page w/ a DataGrid and a dynamic amount of buttons.

Intended functionality: When one of the buttons is clicked, the modal will show, then the asp button click event will fire.

Functionality as of right now: ASP Button is clicked, Modal shows, Event will not fire.

Anyone know if its possible to get both to fire?

Here is my modal:

 div id="example" class="modal hide fade in" style="display: none;">
              <div class="modal-header">
                  <a class="close" data-dismiss="modal">x</a>
                  <h3>Enter Comments Below:</h3>
              </div>
              <div class="modal-body">
                  <h4>Comments</h4>                  
              </div>
              <div class="modal-footer">
                  <a href="#" class="btn btn-success">Call to action</a>  
                    <a href="#" class="btn" data-dismiss="modal">Close</a>                   
              </div>
          </div>

Here is the button:

<asp:Button ID="TestButton2" runat="server" Text="Comments"
OnClick="TestButton_Click"/>

Here is the buttons click event from the code Behind.

protected void TestButton_Click(object sender, EventArgs e)
                        {

                         // This does something

                        }

Here is the Jquery that shows the modal when the button is clicked:

 <script type="text/javascript">
           $('input[name$="TestButton2"]').on("click", function () {

               $('#example').modal('toggle');
               return false;

           });
     </script>

Anyone have any ideas? I've thought about a hidden field but don't know if that would really work. It may just not be possible with WebForms but thought I would ask. Thanks in advance for your help.

回答1:

call java script using ClientScriptManager on your button click

protected void TestButton_Click(object sender, EventArgs e)
                    {

                     // do something then call modal
ClientScript.RegisterStartupScript(GetType(), "Show", "<script> $('#example').modal('toggle');</script>");
                    }