更新面板中的脚本在asp.net中不起作用 [英] Script inside update panel is not working in asp.net

查看:65
本文介绍了更新面板中的脚本在asp.net中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在.aspx页面中使用了一些脚本。在我将更新面板放在这些页面之前,它正在工作。我需要两件事,更新面板和脚本。如何在asp.net的更新面板中启用这些脚本?我正在使用c#和asp.net 3.5框架。

我在页面中使用的脚本是:



1.


I am using some scripts in my .aspx pages. It was working before I put update panel in these pages. I need both things, update panel and scripts. How to enable these scripts inside the update panel in asp.net? I am using c# and asp.net 3.5 framework.
The scripts that I used in the pages are:

1.

Response.Write("<Script>alert('Successfully Updated')</Script>");





2.



2.

string url = "./Quick.aspx";
   string cmd = "window.open('" + url + "', '_blank', 'height=600,width=600,status=yes,toolbar=no,menubar=no,location=yes,scrollbars=yes,resizable=no,titlebar=no' );";
ScriptManager.RegisterStartupScript(this, this.GetType(), "newWindow", cmd, true);
   }







这些代码用于.aspx.cs页面。 />


有关这个问题的任何想法要解决吗?



谢谢....




These codes are used in the .aspx.cs pages.

Any idea about this issue to solve?

Thanks....

推荐答案

// Is a simply way..
//Common.Response.Write("&lt;Script>alert('Successfully Updated')&lt;/Script>");

        string url = "Quick.aspx";
        string cmd = "alert('Successfully Updated');window.open('" + url + "', '_blank', 'height=600,width=600,status=yes,toolbar=no,menubar=no,location=yes,scrollbars=yes,resizable=no,titlebar=no' );";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "newWindow", cmd, true);


试试这个

try this
ScriptManager.RegisterStartupScript(this,this.GetType(),"name", "alert('Successfully Updated');", true);



如果它适用于你,则选择答案。


select as answer if it works for u.


基本上用UpdatePanel插入的脚本是从未执行过他们只是坐在那里什么都不做。插入页面后你必须执行它们。



我为我当前的项目创建了类:

Basically scripts inserted with UpdatePanel are never executed. They are just sitting there and doing nothing. You have to execute them after inserting into page.

I created class for my current project:
public static class UpdatePanelMethods
{

    public static void AddUpdateScript(this UpdatePanel panel, string key, string script)
    {
        if (!panel.Page.ClientScript.IsClientScriptBlockRegistered(key))
        {
            ScriptManager.RegisterClientScriptBlock(
                panel, typeof(UpdatePanel), key, script, true);
        }
    }
}



你这称之为:


You are calling this like:

upSkorowidz.AddUpdateScript("upScript", "foo(bar)");



这不太理想。这是字符串,所以最好简短。

如果你必须执行更复杂的东西,就像常规脚本一样插入它,但将其包含在某个函数中:


It's not ideal. It's string, so it better be really short and simple.
If you have to execute something much more complicated insert it like regular script but enclose this in some function:

<script>
foo(bar){
[whole script]
}
</script>



这样你可以在那个脚本中复杂化并且你有智能感知。

什么是缺点?

你必须在.cs代码中调用这个方法(AddUpdateScript)。 JS scritp必须更复杂,因为你将它修改为.cs文件中的字符串中的简单调用。



如果这不是你的情况,你也可以使用img.error事件。这可能是我职业生涯中写得最丑陋的事情之一:(


That way you can complex things in that script and you got intellisense.
What is downside?
You have to call this method (AddUpdateScript) in .cs code. JS scritp have to be more complicated since you modify this to simple call in string in .cs file.

If this reaaaally not the case for you, you can also use img.error event. It is possibly one of most ugly thing I wrote in my career :(

<%if (Page.IsPostBack)
  {//just for loading scripts %>
<script type="text/javascript" id="someScript">
[some js]
</script>
<img src="dummyImg.jpg" onerror="eval(jQuery('#someScript').text())" alt"" width="0px" height="0px" />
<%} %>



发生了什么事?

没有图片dummyImg.jpg在服务器中。所以浏览器调用执行我们脚本的onerror事件。这是在AJAX之后用HTML注入的html可以执行的两个事件之一。

第二个是frame.onload,但是我不想在我的html中使用框架并且没有测试它。

我读到了object.onload应该可以工作但是我发现这是不真实的。

但它是非常脏的解决方案我会反对它,除非你想要结束DailyWTF:)


What is happening?
There is not image dummyImg.jpg in server. So browser calls onerror event which executes our script. It is one of 2 events that can be executed by html injected in HTML after AJAX.
Second is frame.onload, but I did not want a frame in my html and did not tested it.
I read that also object.onload should work but I have found that untrue.
But it is very dirty solution and I would advice against it unlesss you want to end in DailyWTF :)


这篇关于更新面板中的脚本在asp.net中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆