<< Click to Display Table of Contents >> JQuery - Caixa de confirmação através do código c# |
![]() ![]() ![]() |
Intro
O botão processa no servidor, verifica algumas informações e dependendo de alguma valor, ele mostra uma caixa de confirmação em jQuery (neste caso se o usuário entrar com código 2)
Eu fiz uma gambiarra com um botão invisível por causa do método Page.ClientScript.GetPostBackEventReference(this.btFuncao, string.Empty) não sei se é possível chamar um método direto.
Importante é que funciona
Tela
Tela quando pressiona o botão "Submit" sem digitar nada ou digitando valor diferente de "2" a mensagem "Hora atualizada" sem confirmação nenhuma.
Se digitar o número "2" uma caixa de confirmação aparece:
Ao pressionar "Cancelar" nada acontece. Mas botão "Confirmar" aparece isso:
aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript">
function ShowCaixaDialogo() {
$('#div_confirmacao').dialog({
autoOpen: false,
resizable: false,
modal: true,
buttons: {
"Confirmar": function () { $(this).dialog("close"); <%= Page.ClientScript.GetPostBackEventReference(this.btFuncao, string.Empty) %>; },
"Cancelar": function () { $(this).dialog("close"); }
}
}).dialog("open");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lbInfo" runat="server" Text="Digite 2 para exibir uma pergunta:"></asp:Label>
<asp:TextBox ID="edNumero" runat="server"></asp:TextBox><br />
<asp:Button ID="btnConfirmar" runat="server" Text="Submit" OnClick="btnConfirmar_Click" CausesValidation="true" />
<br />
<asp:Label ID="lbResposta" runat="server" Text="original"></asp:Label>
<asp:Button ID="btFuncao" runat="server" Text="invisivel" OnClick="btFuncao_Click" CausesValidation="true" Style="display: none" />
<div id="div_confirmacao" title="Confirmação!" style="display: none">
<span>Você gostaria de confirmar esta operação?</span>
</div>
</div>
</form>
</body>
</html>
cs
using System;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void AtualizarResposta(string prefixo = "")
{
lbResposta.Text = prefixo + "Hora atualizada: " + DateTime.Now.ToString("mm:ss,fff");
}
protected void btnConfirmar_Click(object sender, EventArgs e)
{
if (edNumero.Text == "2")
Page.ClientScript.RegisterStartupScript(this.GetType(), "idCaixaDialogo", "ShowCaixaDialogo();", true);
else
AtualizarResposta();
}
protected void btFuncao_Click(object sender, EventArgs e)
{
AtualizarResposta("a partir do dialogo - ");
}
}
}