【问题标题】:Javascript on my ASCX control is not redirecting me to a new page/control我的 ASCX 控件上的 Javascript 没有将我重定向到新页面/控件
【发布时间】:2017-05-09 01:52:14
【问题描述】:

我们有一个网站,其中包含所有任务/页面的控件。 每个控件都有一个页面 id (pid),然后我们单击相关链接以转到该页面。

在我的页面上,我有一个为该页面加载数据的按钮,但我想在代码运行之前重定向人们。

本质上,客户端登录并开始订购。当点击添加到购物车按钮时,在代码隐藏将所有商品加载到购物车之前,如果用户没有选择下此订单,则必须警告他们并相应地重定向。

到目前为止,在我的按钮上,我有:

<asp:LinkButton CssClass="Button" Font-Bold="True" Text="Load Lines" runat="server" 
ID="btnVerifyOrder" OnClientClick='checkIsOptedIn();' />

而 javascript 是:

 <script language="javascript" type="text/javascript">
    function checkIsOptedIn() {
        var str = window.location.href;
        var url = str.substring(0, str.length - 2);
        var value = $('#<%=hidIsOptIn.ClientID %>').val();

        if (value == "False") {
            var question = confirm("You are not opted in for ordering to continue processing this. If you would like to view and complete the form, please click OK. Or click Cancel reload the page");
            if (question == true) {
                url = url + "6";
                window.location.replace(url); //this will not work
                window.location.href = url; //or this
            } else {
                url = url + "32";//the current pid of the control. So they just refresh here at the beginning, but this doesn't refresh the page at the top either
                window.location.href = url;
            }
        }
    }
</script>

按钮点击后面也有代码:

 Protected Sub btnVerifyOrder_Click(sender As Object, e As EventArgs) Handles btnVerifyOrder.Click
        Page.MaintainScrollPositionOnPostBack = True
        'Do code work then
        GenerateClaimLines()
 End Sub

现在,当我在本地主机上运行页面并以未选择的客户端身份登录时,我看到了消息并单击“确定”,但未加载被调用的控件。相反,当前页面会加载后面的代码,就好像客户可以下订单一样。

谁能解释一下为什么页面不会重定向。我试图去主机地址,谷歌,任何东西,但我不会被重定向。此外,如果他们单击“取消”,则该页面应重定向到同一页面,但在进程开始时,因此不会加载任何内容,仅加载默认数据。然而这也不加载。

我必须从 jscript 函数返回一个布尔变量吗?还是在这里做点别的?

【问题讨论】:

    标签: javascript jquery asp.net vb.net redirect


    【解决方案1】:

    您需要将return 添加到LinkBut​​ton OnClientClick

    OnClientClick="return checkIsOptedIn();"
    

    那么你的 JavaScript 函数必须返回一个布尔值。

    <script language="javascript" type="text/javascript">
    function checkIsOptedIn() {
        var value = $('#<%=hidIsOptIn.ClientID %>').val();
    
        if (value == "False") {
            //other code
            return false;
        } else {
            return true;
        }
    }
    </script>
    

    【讨论】:

    • 太棒了!现在就像一个魅力。非常感谢。想知道我是否缺少返回值,但我不太确定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多