Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i stop refreshing page in bootstrap modal with asp.net button click

i am trying to show data in modal view of bootstrap, the data is retrieved from the database. The problem is it shows the data but for a fraction of second and the page gets refreshed automatically. this is my html

<div class="col-md-4" style="margin-left: 5px;">
 <br />
 Invoice #<asp:TextBox ID="txt_Invoiceno" runat="server"></asp:TextBox><br />
 <asp:LinkButton ID="lnk_showInvoice" runat="server" OnClick="ShowUserPass_Click" OnClientClick="showModel()">Show Last Five Invoices</asp:LinkButton>
 </div>
<div class="modal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Last five invoices </h4>
            </div>
            <div class="modal-body">
               <asp:GridView runat="server" ID="gvInvoices"></asp:GridView>
            </div>
        </div>
        <div class="modal-footer">
        </div>
    </div>
</div>

this is my .cs file

protected void ShowUserPass_Click(object sender, EventArgs e)
{
    SQLHelper objSql = new SQLHelper();
    objSql.SqlText = "select FirstName,LastName,Email from tblUserDetail";
    DataTable dt = objSql.getDataTable(false);
    gvInvoices.DataSource = dt;
    gvInvoices.DataBind();
    objSql.Close();
    objSql.Dispose();

}

jquery:

 <script>
    function showModel() {
        $('.modal').modal();
    }

</script>

So my question is, how do i stop the page refreshing automatically?

like image 570
AST Avatar asked Aug 19 '16 05:08

AST


People also ask

How do I stop a webpage from reloading the button in asp net?

Page got refreshed when a trip to server is made, and server controls like Button has a property AutoPostback = true by default which means whenever they are clicked a trip to server will be made. Set AutoPostback = false for insert button, and this will do the trick for you.

How do I keep my Bootstrap modal from closing when I click the button?

To prevent closing bootstrap modal when click outside of modal window we need add properties data-backdrop="static" and data-keyboard="false" to button which we are using to show modal window like as shown following.

How do I stop Bootstrap modal pop up?

There are few ways to close modal in Bootstrap: click on a backdrop, close icon, or close button. You can also use JavaScript hide method. Click the button to launch the modal. Then click on the backdrop, close icon or close button to close the modal.


4 Answers

try below code by changing both ASP code and script

<asp:LinkButton ID="lnk_showInvoice" runat="server" OnClick="ShowUserPass_Click" OnClientClick="showModel(); return false">Show Last Five Invoices</asp:LinkButton>


    <script>
        function showModel() {
            $('.modal').modal();

        }

    </script>
like image 187
Jekin Kalariya Avatar answered Oct 12 '22 10:10

Jekin Kalariya


I had same problem. using ajax it solved

html

 <a OnClick="showModel()" >Show Last Five Invoices</a>

Jquery

   function showModel() {
   $('.modal').modal();


    $.ajax({
    type: "POST",
    url: "filename.aspx/ShowUserPass_Click",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    datatype: "jsondata",
    async: "true",
    success: function (t) {
         //set value to gridview or use normal table
    },
    error: function (t) { }
   })
 }
like image 35
User Avatar answered Oct 12 '22 11:10

User


Such problem happens when you are using input type submit but that's not valid for this case.

The second reason is you are using an anchor having a blank href in it. And I think this is happening with you.

To overcome with this problem we can use:

<a href="javascript:void(0)" id="..." class="...">Link</a>

This will not redirect or refresh the page, just act like a button.

like image 25
Mayank Pandeyz Avatar answered Oct 12 '22 11:10

Mayank Pandeyz


Use UpdatePanel as below:

<div class="modal fade" tabindex="-1" role="dialog" id="mdlShowUserPass">
    <div class="modal-dialog" role="document">
        <asp:UpdatePanel runat="server">
            <ContentTemplate>
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">Modal title</h4>
                    </div>
                    <div class="modal-body">
                        Your Content. Ex form
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <asp:Button ID="btnUpdate" runat="server" Text="Update" />
                    </div>
                </div>
                <!-- /.modal-content -->
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    <!-- /.modal-dialog -->
</div>
<!-- /.modal -->

Surround the 'modal-content' class with an UpdatePanel control. You can also open the modal from code behind using the scriptmanager like below.

protected void ShowUserPass_Click(object sender, EventArgs e)
{
    SQLHelper objSql = new SQLHelper();
    objSql.SqlText = "select FirstName,LastName,Email from tblUserDetail";
    DataTable dt = objSql.getDataTable(false);
    gvInvoices.DataSource = dt;
    gvInvoices.DataBind();
    objSql.Close();
    objSql.Dispose();
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "<script>$(function () {$('#mdlShowUserPass').modal({show:true,keyboard: false, backdrop: 'static'});});</script>", false); 
    // note the #mdlShowUserPass id which assigned to the modal
}

If you wish to close the modal from server side, add a LinkButton and assign a click event, use the scriptmanager again to hide the modal by changing to show:false from true.

Edit

I missunderstood your question. If you want to prevent the modal from closing when pressing a button inside the modal, for ex, submit the modal, etc., you can use the aforesaid solution.

However, if you want to open the modal after postback, you can just add the scriptmanager line to the linkbutton click event. No need to add updatepanel to the modal.

like image 45
Ravimallya Avatar answered Oct 12 '22 10:10

Ravimallya