在 jQuery UI 对话框中进行回发
Do a postback in jQuery UI dialog
很多SO用户已经问过这个问题,也有解决方案。但是 none 对我有用。
在 aspx 中
<asp:Button ID="btnConfirm" ClientIDMode="Static" runat="server" Text="Confirm" OnClick="btnConfirm_Click"/>
<script type="text/javascript">
$(function inPageLoad() {
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function () {
//Confirmation Dialog
$(document).on('click', '#btnConfirm', function (e) {
e.preventDefault();
$('<div></div>').appendTo('body')
.html('<div><h6>Yes or No?</h6></div>')
.dialog({
modal: true, title: 'message', zIndex: 10000, autoOpen: true,
width: 'auto', resizable: false,
buttons: {
Yes: function () {
$(this).dialog("close");
__doPostBack('btnConfirm','');
},
No: function () {
$(this).dialog("close");
}
},
close: function (event, ui) {
$(this).remove();
}
});
});
});
});
$(document).ready(inPageLoad);
</script>
在aspx.cs
protected void btnConfirm_Click(object sender, EventArgs e)
{
SomeFunction();
}
如果单击是 'Yes',我想执行回发(转到代码隐藏中的 'btnConfirm_Click' 事件)。尽管对话框很好地弹出,但它不执行回发。感谢所有帮助!
代替__doPostBack('btnConfirm','')
;
Yes: function () {
$("[id*=btnConfirm]").click();
},
更新我的答案
Take another button, with display none property and trigger that
Button
css
.hidden
{
display:none;
}
aspx
<asp:Button id="btnhidden" runat="server" onclick="btnhidden();" cssClass=hidden/>
javascript
Yes: function () {
$("[id*=btnhidden]").click();
},
上述解决方案对我的情况不起作用,所以我想给人们一个替代方案,以防他们最终遇到与我相同的情况 - 以下对我有用:
我想引发的事件来自中继器内的按钮点击 - 所以它使我的情况变得独特。以下是我最终使用的标记:
<asp:Button runat="server" ID="btnPersonRemove" Style="width: 15px" Text="Remove" OnClientClick="return confirmation(this);" UseSubmitBehavior="false" OnClick="btnRemove_Click" />
使用此面板(中继器外):
<asp:Panel ID="pnlConfirmationMessage" runat="server" Style="display: none" CssClass="confirmation">
<asp:HiddenField ID="hdnPersonID" runat="server" />
<asp:Label ID="lblConfirm" runat="server">Are you sure you'd like to remove this member?</asp:Label>
</asp:Panel>
请注意,我将 this
传递给 JS 并从 confirmation
方法返回结果。您将在下面看到传递 this
允许我使用 JS .name
属性.
从单击的控件中检索唯一 ID
<script type="text/javascript">
$(function () {
$("#<%= pnlConfirmationMessage.ClientID %>").dialog({
autoOpen: false,
modal: true,
resizable: false,
draggable: false
});
});
function confirmation(control) {
$("#<%= pnlConfirmationMessage.ClientID %>").dialog('option', 'buttons', {
'Confirm': function () {
$(this).dialog('close');
return evaluate(true, control);
},
'Cancel': function () {
$(this).dialog('close');
return evaluate(false, control);
}
});
$("#<%= pnlConfirmationMessage.ClientID %>").dialog('open');
}
function evaluate(value, control) {
if (value) {
__doPostBack(control.name, '');
}
}
</script>
请注意,在实际引发事件之前,我们不会将按钮绑定到 jQuery 对话框。这会延迟实际发生的回发,直到用户与对话框交互。
如果他们点击jQuery UI对话框中的'Confirm',我们调用evaluate(value)
方法,将true
传给它;如果他们点击 'Cancel' 我们通过 false
.
__doPostBack(id, eventArg)
是启动回发的原因,并附有来自 control.name
的按钮单击事件。
尽管这是在 Repeater 中进行的,但我可以看到它也适用于其他情况。希望这可以帮助某人。
很多SO用户已经问过这个问题,也有解决方案。但是 none 对我有用。
在 aspx 中
<asp:Button ID="btnConfirm" ClientIDMode="Static" runat="server" Text="Confirm" OnClick="btnConfirm_Click"/>
<script type="text/javascript">
$(function inPageLoad() {
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function () {
//Confirmation Dialog
$(document).on('click', '#btnConfirm', function (e) {
e.preventDefault();
$('<div></div>').appendTo('body')
.html('<div><h6>Yes or No?</h6></div>')
.dialog({
modal: true, title: 'message', zIndex: 10000, autoOpen: true,
width: 'auto', resizable: false,
buttons: {
Yes: function () {
$(this).dialog("close");
__doPostBack('btnConfirm','');
},
No: function () {
$(this).dialog("close");
}
},
close: function (event, ui) {
$(this).remove();
}
});
});
});
});
$(document).ready(inPageLoad);
</script>
在aspx.cs
protected void btnConfirm_Click(object sender, EventArgs e)
{
SomeFunction();
}
如果单击是 'Yes',我想执行回发(转到代码隐藏中的 'btnConfirm_Click' 事件)。尽管对话框很好地弹出,但它不执行回发。感谢所有帮助!
代替__doPostBack('btnConfirm','')
;
Yes: function () {
$("[id*=btnConfirm]").click();
},
更新我的答案
Take another button, with display none property and trigger that Button
css
.hidden
{
display:none;
}
aspx
<asp:Button id="btnhidden" runat="server" onclick="btnhidden();" cssClass=hidden/>
javascript
Yes: function () {
$("[id*=btnhidden]").click();
},
上述解决方案对我的情况不起作用,所以我想给人们一个替代方案,以防他们最终遇到与我相同的情况 - 以下对我有用:
我想引发的事件来自中继器内的按钮点击 - 所以它使我的情况变得独特。以下是我最终使用的标记:
<asp:Button runat="server" ID="btnPersonRemove" Style="width: 15px" Text="Remove" OnClientClick="return confirmation(this);" UseSubmitBehavior="false" OnClick="btnRemove_Click" />
使用此面板(中继器外):
<asp:Panel ID="pnlConfirmationMessage" runat="server" Style="display: none" CssClass="confirmation">
<asp:HiddenField ID="hdnPersonID" runat="server" />
<asp:Label ID="lblConfirm" runat="server">Are you sure you'd like to remove this member?</asp:Label>
</asp:Panel>
请注意,我将 this
传递给 JS 并从 confirmation
方法返回结果。您将在下面看到传递 this
允许我使用 JS .name
属性.
<script type="text/javascript">
$(function () {
$("#<%= pnlConfirmationMessage.ClientID %>").dialog({
autoOpen: false,
modal: true,
resizable: false,
draggable: false
});
});
function confirmation(control) {
$("#<%= pnlConfirmationMessage.ClientID %>").dialog('option', 'buttons', {
'Confirm': function () {
$(this).dialog('close');
return evaluate(true, control);
},
'Cancel': function () {
$(this).dialog('close');
return evaluate(false, control);
}
});
$("#<%= pnlConfirmationMessage.ClientID %>").dialog('open');
}
function evaluate(value, control) {
if (value) {
__doPostBack(control.name, '');
}
}
</script>
请注意,在实际引发事件之前,我们不会将按钮绑定到 jQuery 对话框。这会延迟实际发生的回发,直到用户与对话框交互。
如果他们点击jQuery UI对话框中的'Confirm',我们调用evaluate(value)
方法,将true
传给它;如果他们点击 'Cancel' 我们通过 false
.
__doPostBack(id, eventArg)
是启动回发的原因,并附有来自 control.name
的按钮单击事件。
尽管这是在 Repeater 中进行的,但我可以看到它也适用于其他情况。希望这可以帮助某人。