asp.net在ajax updatepanel之后访问javascript变量

时间:2021-04-25 03:37:52

I'm using AJAX on an ASP.NET web project to update a page. Some of my functions return XML that I want to embed on the page after it reloads. This part works, here's a sample of how it looks at the top of the page:

我在ASP.NET Web项目上使用AJAX来更新页面。我的一些函数在重新加载后返回我想要嵌入页面的XML。这部分有用,下面是一个如何查看页面顶部的示例:

var productXML = "<?xml version=\"1.0\"?><ArrayOfProduct xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><Product><ActualProdID>123</ActualProdID><Name>Test</Name><Description>Test</Description><Edition>Test</Edition><Platform>Test</Platform><Family>Test</Family><Type>Test</Type><DeploymentTypes>Test</DeploymentTypes><BaseActualProdID>Test</BaseActualProdID><Price>0</Price></Product></ArrayOfProduct>";

Later in the page I'm trying to use the XML but it's not working. I tried to do something simple and just throw an alert box in that looks like this:

在页面的后面我试图使用XML,但它不起作用。我尝试做一些简单的事情,然后抛出一个警告框,如下所示:

<script type="text/javascript">
function closeLoading()
{
    jQuery('.pleaseWaitPanel').css({ 'display': 'none', 'visibility': 'hidden' });
    alert("here");
    alert(productXML);
    alert("here2");
}
</script>

closeLoading() is called inside:

内部调用closeLoading():

window.onload = function () { Sys.WebForms.PageRequestManager.getInstance().add_endRequest(closeLoading); };

It loads the jQuery and the first alert "here" works perfect. When I go to alert the productXML, nothing happens. It doesn't throw a JavaScript error, I'm using Firebug. I can confirm the XML is on the page.

它加载了jQuery,第一个警告“here”完美无缺。当我去提醒productXML时,没有任何反应。它不会抛出JavaScript错误,我正在使用Firebug。我可以确认XML在页面上。

Any help on this would be GREATLY appreciated!!

任何有关这方面的帮助将非常感谢!!

2 个解决方案

#1


1  

From your code snippets, it looks like your closeLoading function is only being called in your window.onload function. This means that it won't be called after any Ajax request completes as the window won't be reloaded.

从您的代码片段看,您的closeLoading函数似乎只在window.onload函数中调用。这意味着在任何Ajax请求完成后不会调用它,因为窗口不会被重新加载。

I would try moving your call to Sys.WebForms.PageRequestManager.getInstance().add_endRequest(closeLoading) to just before your closing server-side form tag:

我会尝试将您的调用转移到Sys.WebForms.PageRequestManager.getInstance()。add_endRequest(closeLoading)到您关闭服务器端表单标记之前:

<form runat="server">
    ...
    <script>
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(closeLoading);
    </script>
</form>

Hope this helps.

希望这可以帮助。

#2


0  

var productXML 

Is a ServerSide variable, so you don't have access to this in Client script.

是ServerSide变量,因此您无法在客户端脚本中访问此变量。

If you want to use in a javascript function you can do this :

如果你想在javascript函数中使用,你可以这样做:

1) Put the result in a textbox hidden, like

1)将结果隐藏在文本框中,例如

<input type='hidden' value='<%=productXML%>'> 

then simply get the value of the textbox.

然后只需获取文本框的值。

#1


1  

From your code snippets, it looks like your closeLoading function is only being called in your window.onload function. This means that it won't be called after any Ajax request completes as the window won't be reloaded.

从您的代码片段看,您的closeLoading函数似乎只在window.onload函数中调用。这意味着在任何Ajax请求完成后不会调用它,因为窗口不会被重新加载。

I would try moving your call to Sys.WebForms.PageRequestManager.getInstance().add_endRequest(closeLoading) to just before your closing server-side form tag:

我会尝试将您的调用转移到Sys.WebForms.PageRequestManager.getInstance()。add_endRequest(closeLoading)到您关闭服务器端表单标记之前:

<form runat="server">
    ...
    <script>
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(closeLoading);
    </script>
</form>

Hope this helps.

希望这可以帮助。

#2


0  

var productXML 

Is a ServerSide variable, so you don't have access to this in Client script.

是ServerSide变量,因此您无法在客户端脚本中访问此变量。

If you want to use in a javascript function you can do this :

如果你想在javascript函数中使用,你可以这样做:

1) Put the result in a textbox hidden, like

1)将结果隐藏在文本框中,例如

<input type='hidden' value='<%=productXML%>'> 

then simply get the value of the textbox.

然后只需获取文本框的值。