ASP C # Bootstrap - Open MasterPage modal

0

I'm learning to build websites with ASPX C #, using Bootstrap. Before, I have already researched everything and I have not found a solution to the problem I am encountering, so please, if anyone can help me.

In my MasterPage, I have in <header> the modal activation declaration, as follows:

<!-- Bootstrap -->
<link href="../Content/bootstrap.min.css" rel="stylesheet" />
<link href="../Content/font-awesome.min.css" rel="stylesheet" />

<!-- Estilo de Personalização da Página...                       -->
<link href="/Content/PersonalStyle.css" rel="stylesheet" />

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script><scriptsrc="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->

<script type="text/javascript">
    function openModalDanger() {
        $('#ModalDanger').modal('show');
    }

    function closeModalDanger() {
        $('#ModalDanger').modal('hide');
    }
</script>

Then in%% of MasterPage, after setting the menu, comes the code below:

    <asp:ContentPlaceHolder ID="FormBody" runat="server">
    </asp:ContentPlaceHolder>

    <div class="modal fade" id="ModalDanger" role="dialog" aria-labelledby="myModalDanger" runat="server">
        <div class="modal-dialog" role="document" runat="server">
            <div class="modal-content" runat="server">
                <div class="modal-header" style="background-color: red; color: whitesmoke;" runat="server">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4>Atenção!!!</h4>
                </div>
                <div class="modal-body" runat="server" style="margin-top: 25px; padding-top: 20px;">
                    <br />
                    <asp:Label ID="lblDanger1" runat="server" Text="" Visible="True" />
                    <asp:Label ID="lblDanger2" runat="server" Text="" Visible="True" />
                    <asp:Label ID="lblDanger3" runat="server" Text="" Visible="True" />
                    <br />
                    <br />
                </div>
                <div class="modal-footer" runat="server" style="background-color: red;">
                    <%--<button class="btn btn-default" data-dismiss="modal" id="btnDangerClose" runat="server" type="button" >Fechar</button>--%>
                    <asp:Button runat="server" class="btn btn-default" ID="btnDangerCloseAsp" Text="Fechar" OnClick="btnDangerCloseAsp_OnClick" UseSubmitBehavior="false" data-dismiss="modal" />
                </div>
            </div>
        </div>
    </div>

Above <body> , I put a hidden input, if necessary, to save the page targeting link.

    <div>
        <input id="LinkAcess" value="" type="hidden" runat="server" />
    </div>

At the end of MasterPage, I declare jQuery and then Bootstrap as follows:

    </footer>
    <!-- Footer -->
</form>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="../Scripts/jquery-3.1.1.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="/Scripts/bootstrap.min.js"></script>

No Code Behind, I declare the method to activate Modal when needed:

public void MessageDanger(string sTextLine01, string sTextLine02, string sTextLine03, string sLink = "")
        {
            lblDanger1.Text = sTextLine01;
            lblDanger2.Text = sTextLine02;
            lblDanger3.Text = sTextLine03;

            RegisterClientScriptBlock(this, this.GetType(), "MensagemDeAtencao", "$(function() { openModalDanger(); });", true);

            if (!string.IsNullOrEmpty(sLink))
            {
                LinkAcess.Value = sLink;
            }
        }

As seen, this method receives the texts to be presented in the Modal and if, and only if, "sLink" is assigned, it assigns the hidden input.

Still in Code Behind, I declare the method:

    protected void btnDangerCloseAsp_OnClick(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(LinkAcess.Value))
        {
            var sLinkAcess = LinkAcess.Value;
            LinkAcess.Value = "";
            Server.Transfer(sLinkAcess, true);
        }
    }

Assigning the link to a variable and clearing the input; with this, clicking on close, if applicable, redirects to the indicated page.

In the child page, for example, when the access is not valid, I call the method:

    private void AcessoInvalido()
    {
        ((PublicMasterPage)Master).MessageDanger("Por favor, verifique seu e-mail e tente novamente!",
                         "Informações necessárias não encontradas.",
                         "Você será redirecinado a Página Inicial!",
                         "/Default.aspx");
    }

Current situation of the child page, I can access "MessageDanger", which receives the parameters and assigns everything as expected, without errors, passes through the "RegisterClientScriptBlock" line without error, but does not present the modal, in this example opens the page "Default.apsx".

The thing is that it does not open the modal, I already tried calling the script using "RegisterStartupScript" and "RegisterClientScriptBlock", but it did not work.

I tried another tip I found using "UpdatePanel" and it did not work too.

I'm working with MVC and WebForms and whenever I need to put a message on the screen, I want to use modal, hence the Text Labels assignment. When in a validation in the Database, for example, already registered e-mail, CPF already registered, Code non existent, in order, any event that I need to inform the operator or user, I will be using the modal (Danger, Alert and Info) , as well as 2 others for the presentation of explanatory texts as needed, and this, I believe only by Code Behinde, because by APSx I would use the Validators, I think!

How I put it, I'm learning, if it's better, I'm open!

Does anyone know which way to go?

    
asked by anonymous 20.06.2017 / 17:47

1 answer

1

I added the code the way you posted it:

Open / close code of the modal in the masterPage and in the codebehind of the webform I made the call:

ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "openModalDanger();", true);

It turns out that if I leave the script:

function openModalDanger() {
    $('#ModalDanger').modal();
}

Does not open the modal. But then I changed to:

function openModalDanger() {
    setTimeout(function () {
        $('#ModalDanger').modal();
    }, 500);
}

And the modal has opened.

It happened that the call of the webform happened before rendering the HTML of the modal, which was like the last content of the body. So, you just have to bring all the HTML of the modal as the first block of body code in the masterPage. Keep your script in the header and it will work.

Remembering that when you add this script to the head, you need to load the jquery and bootstrap before it, in that order, okay?

My opinion is that I do not think it's cool for the server to call client. Looks like things get minky, do not they? Think about using return variables, all of a sudden, and deal with js checking and calling in js.

follows the test sequence:

MasterPage

<script src="Scripts/jquery-1.10.2.js"></script>
<script src="Scripts/bootstrap.js"></script>
<html lang="pt">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title><%: Page.Title %> - Meu aplicativo ASP.NET</title>
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
    <script type="text/javascript">
        function openModalDanger() {
            $('#ModalDanger').modal();
        }

        function closeModalDanger() {
            $('#ModalDanger').modal('hide');
        }
    </script>
</head>
<body>
    <div class="modal fade" id="ModalDanger" role="dialog" aria-labelledby="myModalDanger" runat="server">
        <div class="modal-dialog" role="document" runat="server">
            <div class="modal-content" runat="server">
                <div class="modal-header" style="background-color: red; color: whitesmoke;" runat="server">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4>Atenção!!!</h4>
                </div>
                <div class="modal-body" runat="server" style="margin-top: 25px; padding-top: 20px;">
                    <br />
                    <asp:Label ID="lblDanger1" runat="server" Text="" Visible="True" />
                    <asp:Label ID="lblDanger2" runat="server" Text="" Visible="True" />
                    <asp:Label ID="lblDanger3" runat="server" Text="" Visible="True" />
                    <br />
                    <br />
                </div>
                <div class="modal-footer" runat="server" style="background-color: red;">
                    <%--<button class="btn btn-default" data-dismiss="modal" id="btnDangerClose" runat="server" type="button" >Fechar</button>--%>
                    <input type="button" />
                </div>
            </div>
        </div>
    </div>
    <form runat="server">
        ...
    </form>
</body>

And the call I tested directly on the default.aspx pageLoad, the first code block of the post:

ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "openModalDanger();", true);
    
20.06.2017 / 21:43