0
I’m learning how to develop websites with ASPX C#, using Bootstrap. Before, I want to say that I’ve researched everything and I haven’t found a solution to the problem I’m encountering, so, please, if anyone can help me.
On my Masterpage, I have <header>
the declaration of modal activation 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>
<script src="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 the <body>
from 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 the <header>
, I have 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>
In Code Behind, I declared the method to activate Modal when necessary:
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, assigns to the hidden input.
Still at Code Behind, I declared 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 the close, if applicable, redirects to the specified page.
On the daughter page, for example, when 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 page daughter, I can access "Messagedanger", which receives the parameters and assigns everything as expected, without errors, goes through the line of "Registerclientscriptblock" 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 tried to call the script using "Registerstartupscript" and "Registerclientscriptblock", but it did not work.
I tried another tip I found using the "Updatepanel" and it didn’t work either.
I am working with MVC and Webforms and whenever I need to put a message on the screen, I want to use the modal, so the Text assignment of Labels. When in a validation in the Database, for example, email already registered, CPF already registered, Nonexistent code, am end, any event that I need to inform the operator or user, I will be using the modals (Danger, Alert and Info), in addition to other 2 for presentation of explanatory texts as necessary, and this, I believe only by Code Behinde, because by Apsx I would use the Validators, I think!
As I put it, I’m learning, if you have a better form, I’m open!
Someone can steer me the right way?
Only one correction:
– Arnaldo Oliveira
Only one fix: public void Messagedanger(string sTextLine01, string sTextLine02, string sTextLine03, string Slink = "") { lblDanger1.Text = sTextLine01; lblDanger2.Text = sTextLine02; lblDanger3.Text = sTextLine03; Registerclientscriptblock(this, this.Gettype(), "Messaging intention", "$(Function() { openModalDanger(); });", true); if (! string.Isnullorempty(Slink)) <=====[ { Server.Transfer(Slink); } }
– Arnaldo Oliveira
And you need to open the modal code Behind even? In which situation you need to open the modal?
– Aline
Whenever I need to put a message on the screen, I want to use the modal, that’s why the Text assignment of Labels. When in a validation in the Database, for example, email already registered, CPF already registered, Nonexistent code, am end, any event that I need to inform the operator or user, I will be using the modals (Danger, Alert and Info), plus 2 for presentation of explanatory texts! And this, I believe only by Code Behinde, because by Apsx I would use the Validators, I think! As I put, I am learning, if it has better shape, I am open!
– Arnaldo Oliveira
Are you using webapi? or webforms?
– Aline
Webforms!......
– Arnaldo Oliveira
You can edit the post and add the info you put in the comment and change the tag.
– Aline