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">×</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?