简体   繁体   English

从下拉列表asp.net中调用javascript方法

[英]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 我有一个下拉列表,当选定的值是一个certin值时,我需要做一些事情,然后重定向到新选项卡中的新页面

now I figured the best way to do this would be to use either response.wirte() like so 现在我想最好的方法是使用其中的任何一个response.wirte()

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()方法调用一个javascript方法,该方法将在不同的标签中打开一个新页面,如下所示

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

and the javascript looks like 和JavaScript看起来像

<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 我已经尝试了这两种方法以及response.write方法,但都无法解析resposnse请求,并且当我使用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 请注意,这两种代码的和平都在我的​​下拉列表的SelectedIndexChanged方法内被淘汰了

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 最后更新 ,我只是屈服了,最后用一个按钮来做后面的代码和Java脚本(不要喜欢那些onclientclick事件:p了),因为似乎没有什么能使Java脚本从下拉菜单的onselected更改中运行。事件

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. 您可以为每个下拉列表值创建一个案例,然后根据这些案例,在新标签中将用户重定向到其他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;
    }
}

You can use Hidden Field instead, in the Code behind, set the Hidden Field Value with the URL. 您可以改用“隐藏字段”,在后面的代码中,用URL设置“隐藏字段值”。

in pageLoad() javascript function check the value of the hidden field, if it is not empty, use the window.open(hiddenFieldValue) something like: 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>

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. 问题在于,在这两种情况下,当页面回发并返回到html时,它们都找不到您的JavaScript函数,即:GoToUrl(),因为它最初并未加载。 You can call window.open(url); 您可以调用window.open(url); method in your C# button click event. C#按钮单击事件中的方法。 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 希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从javascript调用asp.net页面方法无法正常工作 - Calling asp.net page method from javascript not working ASP.NET MVC 5从Javascript Error调用控制器方法 - ASP.NET MVC 5 calling controller method from Javascript Error 使用JavaScript在ASP.NET MVC中的下拉列表 - Dropdown list in asp.net mvc with javascript 下拉更改不调用方法 asp.net mvc jquery - Dropdown change not calling method asp.net mvc jquery 从JavaScript调用ASP.NET EventHandler - Calling an ASP.NET EventHandler from JavaScript ASP.NET:在转发器中填充一个下拉列表(来自数据库),并从转发器数据仓库中为其分配默认值 - ASP.NET: Populate a dropdown list within a repeater (from database), and assign it a default value from the repeater datascource ASP.NET使用javascript检查单选按钮列表值,然后调用ac#方法 - ASP.NET checking radiobutton list values with javascript and then calling a c# method Asp.net下拉onchange调用函数和函数获取下拉选择值 - Asp.net dropdown onchange calling function and within function getting dropdown selected values Javascript / Asp.net在下拉列表后触发事件 - Javascript/Asp.net fire an event after dropdown list Javascript-调用ASP.NET WebService-服务器方法&#39;methodName&#39;失败 - Javascript - calling ASP.NET WebService - The server method 'methodName' failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM