[英]Call code-behind-method from javascript, when user confirms in bootstrap-modal
I have a webform with data and a "Save" button. 我有一个带有数据的Web表单和一个“保存”按钮。
In code behind I have a method where I save data in the DB. 在后面的代码中,我有一种将数据保存在数据库中的方法。
When I click the Save button I first popup a Bootstrap confirmation modal dialog with 2 buttons : Cancel and Yes. 当我单击“保存”按钮时,我首先弹出一个带有2个按钮的Bootstrap确认模式对话框:“取消”和“是”。 The modal pops up on the client side thus saving traffic to the server and back. 该模式会在客户端弹出,从而节省了往返服务器的流量。
Cancel button works fine via the "data-dismiss". 取消按钮可以通过“数据删除”正常工作。
But how do I get the method in the code behind to fire when user clicks the "Yes" button in the confirmation modal? 但是,当用户单击确认模式中的“是”按钮时,如何在后面的代码中触发该方法?
Markup : 标记:
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="btnSave" runat="server" OnClientClick="return ConfirmPopup();" Text="Save" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div class="container">
<div class="row">
<button type="button" style="display: none;" id="jsbtnBS_Confirm_Modal"
data-toggle="modal" data-target="#myBS_Confirm_Modal"
data-backdrop="static" data-keyboard="false">
</button>
<div class="modal fade in" id="myBS_Confirm_Modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body">
<span id="lblBS_Confirm_Modal">You Sure...?</span>
</div>
<div class="modal-footer">
<button type="button" id="btnMethod" class="btn btn-success">Yes - I'm sure</button>
<button type="button" id="btnCancel" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
</div>
</div>
</div>
</div>
Script : 剧本:
function ConfirmPopup() {
$("#jsbtnBS_Confirm_Modal").click();
$('#myBS_Confirm_Modal').on('hidden.bs.modal', function (e) {
return false;
});
}
Code behind : 后面的代码:
protected void btnSave_Click(object sender, EventArgs e)
{
...code to save data in database...
}
I went through dozens of entries and the info there is so confusing... 我浏览了数十个条目,那里的信息如此混乱...
Any idea would be very much appreciated. 任何想法将不胜感激。
EDIT (the solution and a question) : 编辑 (解决方案和问题):
I have just found out that all I have to do is change the "Yes" button in the modal to an ASP button which runs on the server side : 我刚刚发现,我所要做的就是将模式中的“是”按钮更改为在服务器端运行的ASP按钮:
<asp:button ID="btnMethod" runat="server" OnClick="btnSave_Click" class="btn btn-success" Text="Yes"></asp:button>
My question now (since I'm a beginner in web coding) is : Will the modal run on the client side until the "Yes" button is clicked? 现在我的问题(因为我是Web编码的初学者)是: 在单击“是”按钮之前,模式将在客户端运行吗? Or will the definition of this button as runat="server" immediatelly send the whole modal to the server, in which case I loose the benefit of saving traffic to server and back? 还是将这个按钮定义为runat =“ server”会立即将整个模式发送到服务器,在这种情况下,我失去了将流量保存到服务器并返回的好处?
Why using a bootstrap modal? 为什么要使用引导模态? Change the client click to OnClientClick="return confirm('Your confirmation dialog.');" 将客户端单击更改为OnClientClick =“返回确认(“您的确认对话框。”);” it should work. 它应该工作。
I think that you can put runat="server" in the button yes 我认为您可以在按钮中添加runat =“ server”
<button type="button" id="btnMethod" class="btn btn-success" runat="server">Yes - I'm sure</button>
And then, you can know when click on this button. 然后,您可以知道何时单击此按钮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.