jquery - 引导确认对话框是按钮在 ASP.NET 中不起作用
问题描述
我有一个 ASP.NET 按钮,当单击它时,我将 div 显示为模态弹出窗口,如确认对话框。
在确认对话框中,如果用户单击“是”,则需要执行按钮服务器事件。我试过这段代码:
<form id="form1" runat="server">
<div id="modalPopUp" class="modal fade" role="dialog">
<div class="modal-dialog" style="width: 400px">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">
<span id="spnTitle"></span>
</h4>
</div>
<div class="modal-body">
<p>
<span id="spnMsg"></span>.
</p>
</div>
<div class="modal-footer">
<asp:Button type="button" ID="btnConfirm" class="btn btn-danger" runat="server" Text="Yes"
OnClick="btnDelete_Click"></asp:Button>
<button type="button" class="btn btn-default" data-dismiss="modal">NO</button>
</div>
</div>
</div>
</div>
<asp:Button runat="server" ID="btnDelete"
Text="Delete" CssClass="btn btn-danger"
OnClientClick="return getConfirmation(this, 'Please confirm','Are you sure </br> you want to delete?');"
OnClick="btnDelete_Click"/>
<br />
<asp:Literal ID="litMsg" runat="server"></asp:Literal>
</form>
protected void btnDelete_Click(object sender, EventArgs e)
{
// Write the code here to delete the record
litMsg.Text = "Record deleted successfully";
}
<script type="text/javascript">
function getConfirmation(sender, title, message) {
$("#spnTitle").text(title);
$("#spnMsg").html(message);
//$('#modalPopUp').modal({ backdrop: 'static', keyboard: false });
$('#modalPopUp').modal({ keyboard: false });
//$('#btnConfirm').attr('onclick', "$('#modalPopUp').modal('hide');setTimeout(function(){" + $(sender).prop('href') + "}, 50);");
var fClose = function () {
$('#modalPopUp').modal('hide');
};
$("#btnDelete").unbind().one("click", fClose);
return false;
}
</script>
当我单击“删除”按钮时,它会显示带有“是”和“否”按钮的确认对话框。当我单击“否”时,对话框关闭。当我单击“是”时,什么也没有发生。
我的要求是我想执行发件人(正在传递给 getConfirmation 函数)按钮单击服务器事件。
请指导我 - 我在哪里做错了什么?
解决方案
您必须允许按钮的“onclick”事件执行 PostBack。这将引导您到 BackEnd(C#) 代码。您可以通过向AllowPostBack
按钮添加属性来做到这一点。但是当您执行 PostBack 时页面会刷新。它会消失你的模型。
在这种情况下,最好的解决方案是使用 ajax 请求访问后端代码。
推荐阅读
- c# - MQTTnet 客户端发布
- android - 我在使用颤振时遇到了 GeneratedPluginRegistrant.registerWith 方法和 MethodChannel 对象的问题
- c# - 为什么我们需要创建第二个对象
- c++ - 使用递归的矩阵中的最小成本路径
- flutter - Flutter Slider 未在 ExpansionPanel 内部重建
- css - CSS 选择器 - 同一类组中具有 Class 的第一个元素
- java - 如何使用空格键更改边缘(Recrusiv)(JAVA)
- cmd - 编程时如何缩短控制台上显示的错误代码
- ruby-on-rails - 不规则复数的Rails g控制器
- php - Laravel - 将变量传递给布局