R100
R100

Reputation: 481

Call a Javascript Function after UpdatePanel Postback Issue

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

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.

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

call javascript after updatepanel postback

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.

Upvotes: 16

Views: 50167

Answers (3)

Apps Tawale
Apps Tawale

Reputation: 675

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
        }
    });
};

Upvotes: 0

Sydneyandy
Sydneyandy

Reputation: 286

you can add the following code in Page_Load event:

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

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

MSDN

Upvotes: 26

Tahbaza
Tahbaza

Reputation: 9548

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.

Upvotes: 0

Related Questions