Abhishek Jain
Abhishek Jain

Reputation: 315

Open modal Popup on pageload event from server side using c#

I want to show modal popup on certain certain condition on page load event but it is now working.

this is my modalPopup:

<script type="text/javascript">
    function openModal(message, header, url) {
        $('#myModal').modal('show');
        $('#lblMasterMessage').html(header);
        $('#lblMasterbodyMessage').html(message);
        $('#myModal').on('hidden.bs.modal', function(e) {
            if (url != '') {
                window.location = url;
            }
        });
    }
</script>

<asp:Content ID="Content2" ContentPlaceHolderID="BodyContentPlaceHolder" runat="Server">
    <div class="modal fade" id="myModal" runat="server">
        <div class="modal-dialog">
            <div class="modal-content" style="width: 400px; margin: 0 auto;">
                <div class="modal-header" runat="server">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">
                        <label id="lblMasterMessage"></label>
                    </h4>
                </div>
                <div class="modal-body">
                    <label id="lblMasterbodyMessage"></label>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" data-dismiss="modal" runat="server">Close</button>
                </div>
            </div>
        </div>
    </div>
</asp:Content>

Calling modal popup from Backend:

protected void Page_Load(object sender, EventArgs e)
{
     if (dt.Rows.Count > 0)
     {
         BindGrid();
     }
     else
     {
          string message = "Files are not Generated!";
          string header = "Info";
          ScriptManager.RegisterStartupScript(this, this.GetType(), "popup","openModal('" + message + "','" + header + "','MemberHomePage.aspx');", true);
         //ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Business Operation Files are not Generated!'); window.location ='MemberHomePage.aspx';", true);
     }
}

i am not able to show popup when condition is satisfied. Any help will be appreciated.

Upvotes: 1

Views: 5528

Answers (2)

Nino
Nino

Reputation: 7095

div that represents modal runs on server, so ASP.NET generate it's ID. That means that, when page is rendered, modal's ID is not myModal but something like ctl00_Content2_myModal.

Because of that, jQuery cannont find your div on line

$('#myModal').modal('show');

Change that line to

$('#<%= myModal.ClientId %>').modal('show');

or try to access div by its class name, like $(".modal")

Edit: you'll have same problem with other controls which you're trying to find by ID, such as lblMasterMessage, lblMasterbodyMessage etc.

Upvotes: 1

Paresh Maghodiya
Paresh Maghodiya

Reputation: 218

You can try this

// add this code below your javascript function

openModal('<%=message %>', '<%=header %>', 'MemberHomePage.aspx');

Upvotes: 0

Related Questions