在UpdatePanel回发问题之后调用一个Javascript函数

时间:2022-08-25 18:43:15

I basically have in my UpdatePanel a literal that generates a javascript array based on a method in my codebehind.

我在UpdatePanel中有一个文本,它基于codebehind中的一个方法生成一个javascript数组。

I don't have an issue when it comes to loading my content on page load. But if I try and carry out a search to update my javascript array literal within my updatepanel, I found that the literal gets updated on postback after the javascript has already fired.

在页面加载中加载内容时,我没有问题。但是如果我尝试在updatepanel中执行搜索来更新我的javascript数组文字,我发现在javascript已经触发后,文字在回发时被更新。

Here is a basic example of what I have:

我有一个基本的例子:

<script type="text\javascript">
function BindMyFunction(itemList)
{
    //Do something
}
</script>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>  
<!-- Literal containing generated JS array -->
    <asp:Literal ID="ProfileJavscriptOutput" runat="server"></asp:Literal> 
    <ul id="person-search">
    <li><asp:TextBox ID="TxtFirstname" runat="server" Text=""></asp:TextBox></li>
    <!-- Update Literal onClick -->
        <li><asp:ImageButton CssClass="searchbtn" ID="ImageButton1" runat="server" OnClick="ImageButton1_Click" /></li>
    </ul>    
    <!-- Some jCarousel rendered -->
</asp:UpdatePanel>

I've been looking at the following posts:

我一直在看下面的文章:

ASP.NET - UpdatePanel and JavaScript

ASP。NET - UpdatePanel和JavaScript

call javascript after updatepanel postback

在updatepanel postback之后调用javascript。

But I can't seem to apply it correctly to my code.

但是我似乎不能正确地将它应用到我的代码中。

It works fine when I don't use an UpdatePanel. But it is a requirement so that the page position does not move when searches are carried out.

当我不使用UpdatePanel时,它可以正常工作。但这是一个要求,以便在进行搜索时页面位置不会移动。

3 个解决方案

#1


25  

you can add the following code in Page_Load event:

您可以在Page_Load事件中添加以下代码:

ScriptManager.RegisterStartupScript(Me.rptGridAlbum, rptGridAlbum.GetType, "scriptname", "somejavascript", True)

This will fire the javascript on your page after the AJAX callback.

这将在AJAX回调后在页面上触发javascript。

MSDN

MSDN

#2


0  

You could create a simple webservice method that returns the javascript array to the page whenever required and wrap the call to that webservice in a javascript method. Invoking the javascript method to refresh the array in memory on the browser side will work better than expecting the js array literal to be parsed again on UpdatePanel postbacks with any success.

您可以创建一个简单的webservice方法,在需要时将javascript数组返回到页面,并将调用用javascript方法包装到该webservice。在浏览器端调用javascript方法来刷新内存中的数组将比预期js数组文本在UpdatePanel回发中再次被解析成功更好。

#3


0  

var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm != null) {
    prm.add_endRequest(function (sender, e) {
        if (sender._postBackSettings.panelsToUpdate != null) {
            DisplayCurrentTime(); // here your javascript function
        }
    });
};

#1


25  

you can add the following code in Page_Load event:

您可以在Page_Load事件中添加以下代码:

ScriptManager.RegisterStartupScript(Me.rptGridAlbum, rptGridAlbum.GetType, "scriptname", "somejavascript", True)

This will fire the javascript on your page after the AJAX callback.

这将在AJAX回调后在页面上触发javascript。

MSDN

MSDN

#2


0  

You could create a simple webservice method that returns the javascript array to the page whenever required and wrap the call to that webservice in a javascript method. Invoking the javascript method to refresh the array in memory on the browser side will work better than expecting the js array literal to be parsed again on UpdatePanel postbacks with any success.

您可以创建一个简单的webservice方法,在需要时将javascript数组返回到页面,并将调用用javascript方法包装到该webservice。在浏览器端调用javascript方法来刷新内存中的数组将比预期js数组文本在UpdatePanel回发中再次被解析成功更好。

#3


0  

var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm != null) {
    prm.add_endRequest(function (sender, e) {
        if (sender._postBackSettings.panelsToUpdate != null) {
            DisplayCurrentTime(); // here your javascript function
        }
    });
};