确认弹出框未显示 [英] confirm pop up box is not displaying

查看:55
本文介绍了确认弹出框未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。单击提交按钮时,确认弹出框不起作用。有一个文本框需要使用必填字段验证,单击按钮时应显示确认弹出框,单击确定后必须重定向到其他页面。点击后重定向ti其他页面但弹出窗口没有显示。



.aspx.cs文件



 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;

public partial class test:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{

}
protected void Button1_Click( object sender,EventArgs e)
{
// string msg =点击确定确认取消更新;

// Response.Write(< script type ='text / javascript'> confirm(' + msg +');< / script>);

string script = confirm('abc' );;
ClientScript.RegisterClientScriptBlock( this .GetType(), 确认,脚本, true );
Response.Redirect( abc.aspx);

}
}







.aspx file





 <%@     Page    语言  =  C#    AutoEventWireup   =  true < span class =code-attribute>   CodeFile   =  test.aspx.cs   继承  = 测试   % >  

< !DOCTYPE html >

< html xmlns = http://www.w3.org/1999/xhtml >
< head < span class =code-attribute> runat = server >
< title > < / title >
< / head >
< body >
< 表格 id = form1 runat = 服务器 >
< div >
< asp:TextBox ID = TextBox1 runat = 服务器 > < / asp: TextBox >
< asp:RequiredFieldValidator ID = RequiredFieldValidator1 runat = server ControlToValidate = TextBox1 ErrorMessage = 输入名称 > < / asp:RequiredFieldValidator >
< br / >
< asp:按钮 ID = Button1 runat < span class =code-keyword> = server < span class =code-attribute> 文本 = 按钮 OnClick = Button1_Click / >
< / div >
< / form >
< / body >
< / html >

解决方案

警报如果您使用Response.Redirect将无法工作。

您应该在Javascript确认脚本中处理重定向。


试试这样



  string  script =   if(确认('你确定要重定向到abc?'))window.location ='abc.aspx';; 
Page.ClientScript.RegisterStartupScript( this .GetType(), test,脚本, true );


什么你已经完成了很容易在客户端完成。这将为您节省不必要的回发。

Button Control具有 OnClientClick 事件,您可以使用它来调用您的JavaScript代码。

所以你的Button代码将是:

< asp:Button ID =Button1runat =serverText =ButtonOnClientClick =javascript:ConfirmRedirect( ); /> 





在aspx页面的脚本块内部和此函数:

 <   script    < span class =code-attribute> type   =  text / javascript >  
函数ConfirmRedirect(){
var isConfirm = confirm('abc');
if(isConfirm)
window.location.href =abc.aspx;
< / script >





现在为什么你的代码无法工作,你需要使用 RegisterStartupScript()方法而不是 RegisterClientScriptBlock 。后者只是在客户端注册该方法,而前者将在页面完成加载时执行(但在页面的OnLoad事件被引发之前)。



希望帮助!

hello every one. the confirm pop up box is not working when I click on submit button. There is a single text box a required field validation is used, when clicked on button the confirm pop up box should be displayed and after clicking on OK it must redirect to other page. After clicking it redirects ti other page but pop up is not displaying.

.aspx.cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //string msg = "Click OK to Confirm Cancel to Update";

       // Response.Write("<script type='text/javascript'>confirm('" + msg + "');</script>");

        string script = "confirm('abc');";
        ClientScript.RegisterClientScriptBlock(this.GetType(), "Confirm", script, true);
        Response.Redirect("abc.aspx");

    }
}




.aspx file


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="enter name"></asp:RequiredFieldValidator>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

解决方案

Alert Won't work if you are using a Response.Redirect.
You should handle the redirect in the Javascript confirm script itself.


Try like this

string script = "if (confirm('are u sure want redirect to abc ?')) window.location = 'abc.aspx';";
            Page.ClientScript.RegisterStartupScript(this.GetType(), "test", script, true);


What you have done can easily be done on the client side. That will save you an unnecessary postback.
Button Control has OnClientClick event which you can use to call your JavaScript Code.
So your Button code will be:

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:ConfirmRedirect();" />



Inside a script block in your aspx page and this function:

<script type="text/javascript">
        function ConfirmRedirect() {
            var isConfirm = confirm('abc');
            if (isConfirm)
                window.location.href = "abc.aspx";
</script>



Now about why your code is not working, you will need to use RegisterStartupScript() method instead of RegisterClientScriptBlock. The latter would just register the method on the client side whereas the former would execute when the page finishes loading (but before the page's OnLoad event is raised).

Hope that helps!


这篇关于确认弹出框未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆