【问题标题】:Javascript - Not executing when used with C# Response.Redirect()Javascript - 与 C# Response.Redirect() 一起使用时不执行
【发布时间】:2015-06-25 09:48:43
【问题描述】:

我有一个带有按钮的用户控件,单击按钮时我想使用ScriptManager 执行Javascript 弹出窗口并返回父页面执行response.redirect。但由于某种原因,我的Javascript 没有被执行。我注意到,如果我删除父级上的响应重定向,Javascript 执行工作正常。

下面是我的代码:

用户控制:

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

namespace RegisterDemo
{
    public partial class RegisterUserControl : System.Web.UI.UserControl
    {

        public delegate void customHandler(object handler);
        public event customHandler SendtoParent;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void usercontrolbutton_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "validator", "alert('Error');", true);
            SendtoParent(sender); 

        }
    }
}

父页面:

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

namespace RegisterDemo
{
    public partial class _Default : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            MyUserInfoBoxControl.SendtoParent += new RegisterUserControl.customHandler(onusercontrolevent_click);
        }

        protected void DemoButton_Click(object sender, EventArgs e)
        {
            //ScriptManager.RegisterStartupScript(this, GetType(), "validator", "alert('u click this button');", true);
        }

        protected void onusercontrolevent_click(object sender)
        {

            ScriptManager.RegisterStartupScript(this, this.Page.GetType(), "event", "alert('event completed');", true);
            Response.Redirect("www.google.com");
        }
    }
}

有人可以帮我找出如何让我的UserControl 中的Javascript 执行吗?我在这里错过了什么?

【问题讨论】:

  • 你不能这样做 - 重定向首先发生
  • 我明白,但我的问题是为什么用户控件中的 javascript 语句没有执行?这个:ScriptManager.RegisterStartupScript(this, GetType(), "validator", "alert('Error');", true);

标签: javascript c# asp.net .net response


【解决方案1】:

您可以使用 JavaScript 进行重定向。例如,window.location.href = "" 而不是使用后面代码中的Response.Redirect

【讨论】:

    【解决方案2】:

    正如其他人所说,您的重定向首先发生。可以使用以下任意一种到 JS 的方法进行重定向

    // similar behavior as an HTTP redirect
    window.location.replace("http://google.com");
    
    // similar behavior as clicking on a link
    window.location.href = "http://google.com";
    

    创建一个新函数

    function Validation() {
        alert('Error');
    
        // similar behavior as an HTTP redirect
        window.location.replace("http://google.com");
    
        // similar behavior as clicking on a link
        window.location.href = "http://google.com";
    
    }
    

    然后改变你ScriptManager

    ScriptManager.RegisterStartupScript(this, GetType(), "validator", "Validation();", true);
    

    【讨论】:

    • 我明白,但我的问题是为什么用户控件中的 javascript 语句没有执行?这个:ScriptManager.RegisterStartupScript(this, GetType(), "validator", "alert('Error');", true);
    • 因为那段代码在客户端运行,它只是跳过它并运行 SendtoParent(sender);在警报运行之前。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 2021-02-26
    • 2011-02-22
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    相关资源
    最近更新 更多