我有一个带有提交按钮的表单。单击“提交”按钮后,我会显示一个带有“确认”和“取消”的 jqueryUI 对话框。单击“确认”后,我想回发并调用提交按钮的事件处理程序而不是 Page_Load 事件。
我有以下内容
HTML
<input type="submit" value="Submit" id="submit" runat="server" onclick="submit" />
JQuery
$("#dialog").dialog('option', 'buttons', {
"Confirm": function () {
$("#ctl01").submit();
},
"Cancel": function () {
$(this).dialog("close");
}
});
到目前为止,这将调用 page_load 事件,因为我“刚刚”提交整个表单。
服务器端
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack)
result.Text += "hey post-back, we did it!";
}
protected void submit(object sender, EventArgs e)
{
result.Text += "hey, event handler! ";
}
最佳答案
从您的帖子中我了解到您想在单击对话框中的按钮时简单地转到服务器端事件。
因此,我创建了一个隐藏
ASP 服务器按钮
,并通过“确认”按钮触发它的单击。
这是工作代码。
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="CSS/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css" />
<script src="JS/jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="JS/jquery-ui-1.8.16.custom.min.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div id="dialog" style="display: none" title="Empty the recycle bin?">
<p>
<span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>
<script>
jQuery(function() {
var dlg = jQuery("#dialog").dialog({
draggable: true,
resizable: true,
show: 'Transfer',
hide: 'Transfer',
width: 320,
autoOpen: false,
minHeight: 10,
minwidth: 10,
buttons: {
"Confirm": function() {
$("#<%=btnNew.ClientID %>").click();
},
"Cancel": function() {
$(this).dialog("close");
}
}
});
dlg.parent().appendTo(jQuery("form:first"));
});
jQuery(document).ready(function() {
jQuery("#submit").click(function(e) {
jQuery('#dialog').dialog('open');
});
})
</script>
<input type="button" value="Submit" id="submit" />
<asp:Button runat="server" Style="display: none" ID="btnNew" Text="Submit" OnClick="btnNew_Click" />
</form>
</body>
</html>
在_Default.asp.cs
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnNew_Click(object sender, EventArgs e)
{
result.Text += "hey, event handler! ";
}
}
关于JQuery UI 对话框和 Asp(Web 表单)调用事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7913189/