Using jQuery and ScriptManager to update dynamic page elements
I recently came across a situation where an element on the page was not updating dynamically after post backs.
It was quick and easy to add an update panel and the necessary logic to use the built in asp .Net ajax callbacks.
However, this caused some conflicts with some other pre-existing ajax code and modal dialogs, so I turned to another solution.
Rather than re-work these controls and methods which were already functioning correctly and extensively implemented, I added a small snippet of code on page load for the content pages which I wanted to trigger a force reload. This did the trick.
ScriptManager.RegisterStartupScript(Me, Me.GetType(), "loadminicart", "$('#minicart').load('" & _ strRootSiteURL & "minicartdata.aspx');", False)
ScriptManager.RegisterStartupScript(this, this.GetType(), "loadminicart", "$('#minicart').load('" + strRootSiteURL + "minicartdata.aspx');", false);
“minicartdata.aspx” is a dynamic page with no masterpage, header, footer or body HTML tags specified – only div’s and content elements.
“minicart” is the id of the div which minicartdata.aspx will be loaded into.
In my specific instance, there were only two pages which I needed to force the minicart content to reload. As a requirement, I needed the reload to occur after an operation on these pages, so since there were only two pages, it was easy to specify this code in both.
In some cases, you may want to be able to easily trigger a refresh from code behind without having to place the above code in every page. For this I recommend simply creating a global function and placing the snippet above, then calling the global function from anywhere needed.
Another, more object oriented approach is to create your own type which inherits page, and use this as the base type for all your content pages. You could then place a function in your new type which contains the snippet above. This effectively has the same end result as using a global function.
Hope this information can be as useful to someone else as I found it to be. 😉
Leave a comment