我正在编写一个管理页面,其中有一个用户名文本框和一个刷新按钮,用于显示单击时的用户详细信息。在其他控件中,我有一个“删除用户”按钮来删除用户。在尝试此操作之前,我有javascript代码可以获得确认。 因为我不希望此“删除用户”按钮具有提交行为,所以我设置了UseSubmitBehavior = False。但是,这导致服务器端事件不会被触发。所以我写了一个小的客户端函数来显式调用__doPostBack。
最终代码有效,但仅适用于基于母版页或嵌套母版页的不的页面。可悲的是,我的所有网页都是基于母版页。 想知道我是否遗漏了什么或是否有办法解决?我刚刚启动了asp.net,请原谅任何明显的错误。
非常感谢提前。
以下代码有效(没有母版页的webform)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm_with_NoMasterPage.aspx.cs" Inherits="WebApplication1.WebForm_with_NoMasterPage" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<script type="text/javascript">
function GetConfirmation(btn, msg) {
if (confirm(msg)) {
__doPostBack(btn.id, '');
return true;
}
else {
return false;
}
}
</script>
<asp:Button ID="btnRemoveUser" runat="server" Text="Remove User" OnClick="btnRemoveUser_Click"
OnClientClick="return GetConfirmation(btnRemoveUser, 'Really remove?');"
UseSubmitBehavior="false"
Height="37px" Width="230px" />
</div>
</form>
</body>
</html>
代码背后的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm_with_NoMasterPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRemoveUser_Click(object sender, EventArgs e)
{
// put a breakpoint here and see if it is hit.
int x = 0;
}
}
}
以下不起作用(相同的片段,但在基于母版页的Webform中):
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm_WITH_MasterPage.aspx.cs" Inherits="WebApplication1.WebForm_WITH_MasterPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
function GetConfirmation(btn, msg) {
if (confirm(msg)) {
__doPostBack(btn.id, '');
return true;
}
else {
return false;
}
}
</script>
<asp:Button ID="btnRemoveUser" runat="server" Text="Remove User" OnClick="btnRemoveUser_Click"
OnClientClick="return GetConfirmation(btnRemoveUser, 'Really remove?');"
UseSubmitBehavior="false"
Height="37px" Width="230px" />
</asp:Content>
代码背后:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class WebForm_WITH_MasterPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnRemoveUser_Click(object sender, EventArgs e)
{
// put a breakpoint here and see if it is hit
int z = 0;
}
}
}