我在aspx页面上有两个脚本。但是当“closeWindow”被一个asp:按钮调用时,它会显示“Email Sent”消息,但会关闭窗口。
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Button runat="server" Text="Close Window" id="closewindowbutton" onclick="closewindowbutton_Click" class="btn" />
</asp:Content>
protected void closewindowbutton_Click(object sender, System.EventArgs e)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("closeWindow"))
{
ScriptManager.RegisterStartupScript(page, typeof(Page), "closeWindow", "window.close();", true);
// ClientScript.RegisterStartupScript(typeof(Page), "closeWindow", "window.close();", true);
}
}
public static void ShowAlert(string message)
{
string cleanMessage = message.Replace("'", "\'");
Page page = HttpContext.Current.CurrentHandler as Page;
string script = string.Format("alert('{0}');", cleanMessage);
if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
{
ScriptManager.RegisterStartupScript(page, typeof(Page), "alert", script, true);
}
return;
}
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
在页面加载时显示“已发送电子邮件”消息:
protected void Page_Load(object sender, System.EventArgs e)
{
ShowAlert("Email Sent!");
}