0

I have a drop down list that when the selected value is of a certin value I need to do some stuff and then redirect to a new page in a new tab

now I figured the best way to do this would be to use either response.wirte() like so

Response.Write(<script>);
Response.Write(window.open('url'));
Response.Write(</script>);

or Page.ClientScript.RegisterStartupScript() methode to call a javascript methode that would open a new page in a diffrent tab like so

Page.ClientScript.RegisterStartupScript(GetType(),"GoToURL", "<script language=JavaScript>GoToURL(" + url + ")</script>");

and the javascript looks like

<script type="text/javascript">
                function GoToURL(url)
                {
                    window.open(url);
                }
            </script>

I have tried both of these and the response.write methode just coming up with unable to parse the resposnse request and nothing at all happens when I use the Page.ClientScript.RegisterStartupScript

can anyone see where I have gone wrong with this or of any other possible way to do this

Note that both of these peaces of code were exicuted inside the SelectedIndexChanged method of my drop downlist

Thanks in advance

Update in the end I just caved in and filnally used a button to do the code behind and java script (got to love those onclientclick events :p) as nothing seems to be working to get java script to run from a drop down menu's onselected changed event

Thanks for all your suggestion though :)

4

3 回答 3

0

编辑:

根据您的评论,我认为这是您正在寻找的解决方案。您可以为每个下拉列表值创建一个案例,并根据这些案例将用户重定向到新选项卡中的不同 url。

    protected void ddlAuthoritytype_SelectedIndexChanged(object sender, EventArgs e)
{
    var port = Request.Url.IsDefaultPort ? "" : ":" + Request.Url.Port.ToString();

    string ddl = ddlAuthoritytype.SelectedValue;
    switch (ddl)
    {
        case "AL":
            var script = string.Format("window.open('{0}://{1}{2}{3}')", Request.Url.Scheme, Request.Url.Host, port, ResolveUrl("~/alabama-state-tax-calculator.aspx"));
            ScriptManager.RegisterStartupScript(this, this.GetType(), "newWindow",script, true);
            break;
    }
}
于 2013-04-03T16:00:06.437 回答
0

您可以改用隐藏字段,在后面的代码中,使用 URL 设置隐藏字段值。

pageLoad()javascript 函数中检查隐藏字段的值,如果它不为空,请使用以下内容window.open(hiddenFieldValue)

<script type="text/javascript">
    function pageLoad()
    {
        // Use $('#' + '<%= hfURL.ClientID %>') if hidden field has runtat="server"
        if($('#' + '<%= hfURL.ClientID %>').val()) 
            window.open($('#' + '<%= hfURL.ClientID %>').val()); 
            // OR window.open($("hfURL").val(), '_blank');
    }
</script>
于 2013-04-03T16:05:36.883 回答
0

问题是,在这两种情况下,当页面回发并返回到 html 时,他们都没有找到您的 JavaScript 函数,即:GoToUrl(),因为它最初没有被加载。您可以在 C# 按钮单击事件中调用window.open(url);方法。像这样:

Page.ClientScript.RegisterStartupScript(GetType(),"GoToURL", "<script language=JavaScript>window.open(" + url + ");</script>");

我没有尝试过,但我认为这应该有效。

希望这会有所帮助

于 2013-04-03T16:11:15.463 回答