Total Pageviews

Monday 25 January 2016

Bootstrap confirm dialog in asp.net server control

Objective : We use here a html control and server control to activate a confirmation dialog(Bootstrap modal)

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>



    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>



    <script>

        $(document).ready(function () {
            $('a[data-confirm]').click(function (ev) {
                var href = $(this).attr('href');
                if (!$('#dataConfirmModal').length) {

                    $('body').append('<div id="dataConfirmModal" class="modal fade" role="dialog" aria-labelledby="dataConfirmLabel" aria-hidden="true"><div class="modal-header" style="background: orange; color: white;"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h3 id="dataConfirmLabel">Please Confirm</h3></div><div class="modal-body" style="background: white;"></div><div class="modal-footer"><button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button><a class="btn btn-primary" id="dataConfirmOK">OK</a></div></div>');
                }
                $('#dataConfirmModal').find('.modal-body').text($(this).attr('data-confirm'));
                $('#dataConfirmOK').attr('href', href);
                $('#dataConfirmModal').modal({ show: true });
                return false;
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">

        <a href="delete.cfm" data-confirm="Are you sure you want to delete?">Delete</a>

        <asp:LinkButton ID="LinkButton1" runat="server" data-confirm="Are you sure you want to delete?" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>


   
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

    </form>
</body>
</html>


Code Behind
-----------------
protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Write("ok");
    }