简体   繁体   English

ASP.NET UpdatePanel PreRender问题

[英]ASP.NET UpdatePanel PreRender issues

I have a website which uses ASP.NET C# for the server side scripting, of which mostly all of the server controls are dynamically added to an UpdatePanel control on the page during the Page_Init event. 我有一个网站,该网站使用ASP.NET C#进行服务器端脚本编写,其中几乎所有服务器控件都在Page_Init事件期间动态添加到页面上的UpdatePanel控件中。 The following snippet of code: 以下代码段:

contentPanel.ContentTemplateContainer.Controls.Add(new LiteralControl("<script>" + clientScriptText + "</script>"));

exists in my Page_Init event (contentPanel is the name of my UpdatePanel). 存在于我的Page_Init事件中(contentPanel是我的UpdatePanel的名称)。 The idea is that this will allow me to run the JavaScript 'alert()' function from the code behind - by setting "alert(\\"--string--\\")" as the value of 'clientScriptText'. 想法是,这将允许我从后面的代码运行JavaScript'alert()'函数-通过将“ alert(\\”-string-\\“)”设置为'clientScriptText'的值。 I have tested this by putting this code in the UpdatePanel's PreRender event (since it is the UpdatePanel and not the page's PreRender event it shouldn't be ASP.NET page lifecycle factors which are causing the problem, should it?), but although the event fires, when the page is rendered no alert appears. 我已经通过将这段代码放在UpdatePanel的PreRender事件中进行了测试(因为它是UpdatePanel而不是页面的PreRender事件,所以它不应该是导致问题的ASP.NET页面生命周期因素,应该吗?),尽管事件触发,呈现页面时不显示任何警报。 My code for this is shown below: 我的代码如下所示:

protected void contentPanel_PreRender(object sender, EventArgs e)
        {
            clientScriptText = "alert(\"Hello\")";
            contentPanel.Update();  //Doesn't work with or without this
        }

Can anybody see what might be wrong? 有人可以看到哪里出了问题吗? Thank you. 谢谢。

You have to run your JavaScript code with a ScriptManager . 您必须使用ScriptManager运行JavaScript代码。 UpdatePanel has its own specific lifecycle and rendering techniques, and the ScriptManager was created exactly to solve problems with JS code. UpdatePanel具有其特定的生命周期和呈现技术,而ScriptManager正是为解决JS代码问题而创建的。 Outside an UpdatePanel you should use the ClientScriptManager . 在UpdatePanel之外,您应该使用ClientScriptManager

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

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