我需要在触发按钮的click事件之前调用jquery函数,但就我而言,在调用jquery函数之前触发了click事件。 我的按钮是:
<asp:Button ID="btnConfrm" runat="server" Text="View" ForeColor="Black" Width="80px" CssClass="button" Height="30px" ValidationGroup="btn" OnClientClick="ConfirmDialog()" OnClick="OnConfirm" />
JQuery函数是:
<script type="text/javascript">
var confirm_value = document.createElement("INPUT");
confirm_value.type = "hidden";
confirm_value.name = "confirm_value";
function ConfirmDialog() {
$('<div></div>').appendTo('body')
.html('<div><h4>' + 'Do you wanto to see previous data' + '?</h4></div>')
.dialog({
modal: true, title: 'Carry forward!', zIndex: 10000, autoOpen: true,
width: 'auto', resizable: false,
buttons: {
Yes: function () {
// $(obj).removeAttr('onclick');
// $(obj).parents('.Parent').remove();
$('body').append('<h1>Confirm Dialog Result: <i>Yes</i></h1>');
$(this).dialog("close");
confirm_value.value = "Yes";
},
No: function () {
$('body').append('<h1>Confirm Dialog Result: <i>No</i></h1>');
$(this).dialog("close");
confirm_value.value = "No";
}
},
close: function (event, ui) {
$(this).remove();
}
});
document.forms[0].appendChild(confirm_value);
};
</script>
事件是:
Public Sub OnConfirm(ByVal sender As Object, ByVal e As EventArgs) Handles btn_show.Click
Dim confirmValue As String = Request.Form("confirm_value")
Session("confirm") = confirmValue
If confirmValue = "Yes" Then
gvCustomers.Visible = True
grdblank.Visible = False
Fill_grid()
Else
gvCustomers.Visible = False
grdblank.Visible = True
'End If
End If
End Sub
我从jquery函数中获取了Confirm_value,但是在按钮中单击事件的值是空的。
答案 0 :(得分:1)
问题是,当用户单击按钮时,将调用您的javascript方法,但此后将立即发生服务器回发,并且对话框将不再存在。因此,您需要某种方式在回发发生之前显示对话框。
有很多方法,这是一个示例。它将通过向OnClientClick中添加“ return false”(OnClientClick =“ ConfirmDialog(); return false; ”)以及用户是否单击“是”来取消原始回发。 ”,它将执行回发。这样,只有当用户单击“是”时,服务器端的按钮单击事件才会执行:
页面代码:
<asp:Button ID="btnConfrm" runat="server" Text="View" ForeColor="Black" Width="80px" CssClass="button" Height="30px" ValidationGroup="btn" OnClientClick="ConfirmDialog(); return false;" OnClick="OnConfirm" />
<script type="text/javascript">
function ConfirmDialog() {
$('<div></div>').appendTo('body')
.html('<div><h4>' + 'Do you wanto to see previous data' + '?</h4></div>')
.dialog({
modal: true, title: 'Carry forward!', zIndex: 10000, autoOpen: true,
width: 'auto', resizable: false,
buttons: {
Yes: function () {
<%= ClientScript.GetPostBackEventReference(btnConfrm, Nothing) %>
},
No: function () {
$(this).dialog("close");
}
},
close: function (event, ui) {
$(this).remove();
}
});
};
</script>
在服务器端:
Public Sub OnConfirm(ByVal sender As Object, ByVal e As EventArgs) Handles btn_show.Click
gvCustomers.Visible = True
grdblank.Visible = False
Fill_grid()
End Sub
答案 1 :(得分:0)
更改OnClientClick =“ return ConfirmDialog();”
<asp:Button ID="btnConfrm" runat="server" Text="View" ForeColor="Black" Width="80px" CssClass="button" Height="30px" ValidationGroup="btn" OnClientClick="return ConfirmDialog();" OnClick="OnConfirm" />