简体   繁体   English

RegisterStartupScript不再在asp.net中工作

[英]RegisterStartupScript no longer working in asp.net

Mostly my website is working with ajax, but if I need to send a message on page load I want it to be in the same style as everywhere else, so I am using the following method on my master page: 大多数情况下,我的网站都使用ajax,但是如果我需要在页面加载时发送消息,则希望它与其他任何地方都具有相同的样式,因此我在主页上使用以下方法:

public void showWarning(String message)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(),"svrShowWarning","showWarning('" + message + "');",true);
}

This was working fine, but recently it has stopped working (I don't know when, exactly.) 这个工作正常,但是最近它停止工作了(确切的时间我不知道。)

The server simply fails to put the script on the page at all - looking at the page source in my web browser, I can see that the script is not present. 服务器根本无法将脚本放在页面上-在我的Web浏览器中查看页面源,可以看到该脚本不存在。

Before, I had a form with a runat=server tag enveloping all the content on my master page, but I took this out. 以前,我有一个带有runat=server标记的表单,该表单包围了母版页上的所有内容,但是我将其取出来。 I wondered if this may be the cause? 我想知道这可能是原因吗?

What is a good way to get some simple javascript like this to fire on my page, after everything has loaded? 在加载完所有内容后,如何在页面上触发类似这样的简单javascript的好方法?

As ASP.NET Webforms needs a form with a runat=server attribute in order to work correctly, this could be the cause. 由于ASP.NET Webforms需要具有runat=server属性的表单才能正常工作,所以这可能是原因。

Having a look at the documentation , it says that the script is added just before the element 看一下文档 ,它说脚本是在元素之前添加的

so you probably need that form. 因此您可能需要该表格。

Regarding running the script when everything has been loaded, you could fire it in the onload event of the window object: 关于在加载所有内容后运行脚本,可以在window对象的onload事件中触发它:

public void showWarning(String message)
{
    string script = "window.onLoad(function(){showWarning('" + message + "');});";
    Page.ClientScript.RegisterStartupScript(this.GetType(),"svrShowWarning",script",true);
}

if you are using jQuery on your page, you could use 如果您在页面上使用jQuery,则可以使用

string script = "$(document).ready(function(){showWarning('" + message + "');});";

instead 代替

With Ajax, using ScriptManager to register scripts is reccomended: 对于Ajax,建议使用ScriptManager注册脚本:

ScriptManager.RegisterStartupScript(Page, GetType(),
       "svrShowWarning","showWarning('" + message + "');",true);

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

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