Click here to Skip to main content
15,899,935 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to fire a Bootstrap Modal at the end of a section of code behind by calling a Javascript function, it will run the alert() that is in the function but steadfastly refuses to display any modal I try.

Code behind partial :-

ScriptManager.RegisterStartupScript(Me, Page.GetType(), "Script", "openModal();", True)


And the Javascript Function on the content page :-

<%@ Page Title="" Language="vb" AutoEventWireup="False" MasterPageFile="~/Site.Master" CodeBehind="clientprocess.aspx.vb" Inherits="UPARS.clientprocess" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>

    <asp:ScriptManager ID="ScriptManager1" runat="server"
        EnablePageMethods="true">
    </asp:ScriptManager>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    <style>
        .GridViewClass td, th {
            padding: 4px;
        }
    </style>


    <script type="text/javascript">

        function openModal() {
            alert("howdy");
           $('#chqPayModal').modal('show');
        }

    </script>


And the Modal code:-

<pre>   <div class="modal modal-dialog-centered fade " id="chqPayModal" tabindex="-1" role="dialog" aria-labelledby="chqPayModal" aria-hidden="true">
        <div class="modal-dialog " role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true"</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-md-3"></div>
                        <div class="col-md-5">
                            <div class="form-group">
                                <input type="text" class="form-control" id="chqNumber" placeholder="Cheque Number">
                            </div>
                            <div class="form-group">
                                <input type="text" class="form-control" id="chqAmount" placeholder="Cheque Value">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" runat="server" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="button" onclick="chqTransfer();" class="btn btn-primary">Save </button>
                </div>
            </div>
        </div>
    </div>


What I have tried:

I can fire this modal no problem with a button on the content page, but not from code behind, although the alert() part does fire.

Hope someone has some idea as I surely don't ....
Posted
Comments
F-ES Sitecore 13-Apr-18 5:29am    
First thing you need to appreciate is that you can't call js from code-behind, your code-behind runs on the server and generates output which is then sent to the browser to run. All your RegisterStartupScript is doing is adding js to the output that you want to run when the browser receives the finished page.

If the "alert" is running but the modal isn't showing then it's probably because it can't find it by id. We can't say why that is, you'll need to look at the source of the page and try to work it out. You can debug if this is the issue using code like

function openModal() {
alert($('#chqPayModal').length);
}

if the alert shows "0" then that's the problem.
Member 10111160 13-Apr-18 8:19am    
Hi
Thanks for taking the time to respond, I added the debug code but whilst the first alert still runs, the alert($('#chqPayModal').length); fails to run at all, so no 2nd alert box comes up at all.

If I just put your debug code in alone in the function, nothing happens with regard to an alert firing....
F-ES Sitecore 13-Apr-18 8:32am    
If the code doesn't run at all that's normally because there is a syntax error. Check the error console of the browser, maybe you're not loading the jQuery library properly or maybe there is an issue elsewhere.
Member 10111160 13-Apr-18 8:49am    
It seems that it is the Jquery it wont run, my plain and simple JavaScript alert runs fine.

I load these in the Site.Master files :-

<!-- REQUIRED JS SCRIPTS -->

<!-- jQuery 3 -->

<!-- Bootstrap 3.3.7 -->

<!-- AdminLTE App -->


These must be functioning as all the rest of the Bootstrap and Modals work fine on the content page.
F-ES Sitecore 13-Apr-18 9:07am    
No point in making assumptions, use the network tab of the browser tools to ensure the scripts are loading and check the console for errors.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900