简体   繁体   中英

calling a javascript method from within a dropdown list asp.net

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 :)

Edit:

Based on your comment, I think this is the solution your looking for. you can create a case for each dropdown list value and according those cases, redirect users to different url in a new tab.

    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;
    }
}

You can use Hidden Field instead, in the Code behind, set the Hidden Field Value with the URL.

in pageLoad() javascript function check the value of the hidden field, if it is not empty, use the window.open(hiddenFieldValue) something like:

<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>

The problem is that in both conditions when page is post back and return to html they have not found your JavaScript function ie: GoToUrl() because it is not initially loaded. You can call window.open(url); method in your C# button click event. like this:

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

I have not tried this but I think this should worked.

Hope this will help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM