Showing posts with label library. Show all posts
Showing posts with label library. Show all posts

Wednesday, March 28, 2012

calling external javascript functions/ library from within an update panel

I have an application with a large number of javaScript functions that reside in external .js files. User controls that reference these files work perfectly.

When usercontrols inside updatepanels call the external javascript functions and libraries, they fail with 'object not found' errors.

I tested using the scriptmanager.registerclientscriptblock by adding a concatenated string with a simple function and it worked. But this is a bad way to do things both from a debugging standpoint as well as ease of code. There must be a better way?

Dim scriptTextAsString

scriptText = _

"function prepareToClose() {"

& _

"alert('prepare to close');"

& _

"test();"

& _

" }"

System.Web.UI.ScriptManager.RegisterClientScriptBlock(

Me.btnLoad,Me.GetType(),"prepare_close", scriptTextTrue)

btnLoad.Attributes.Add(

"onCLick","prepareToClose(); return false;")

What is the best way to do this?

Unless someone has a better solution, here is what I did. Basically, I load the external file into a string and use the rgisterScriptBlock...everything works well and there is still just 1 instance of the scripts for easy maintenance and other non-ajax pages can use them. Here is the code for anyone interested.:

System.Web.UI.ScriptManager.RegisterClientScriptBlock(Me.btnClose, Me.GetType(), "objectives", regLongScript("objectives.js"), True)


...

Private Function regLongScript(ByVal incoming_name As String) As String
Dim objStreamReader As IO.StreamReader
Try
Dim file_name As String = ""
file_name = Request.PhysicalApplicationPath & "a\js_scripts\" & incoming_name
objStreamReader = File.OpenText(file_name)
Return objStreamReader.ReadToEnd()

Catch ex As Exception
Return ""
Finally
objStreamReader.Close()
End Try
End Function


Here's how I've been doing it:

ScriptManager.RegisterStartupScript(

UpdatePanelTest,

UpdatePanelTest.GetType(),

"keyValue",

"alert('test')",

true);


Here's how I've been doing it:

ScriptManager.RegisterStartupScript(

UpdatePanelTest,

UpdatePanelTest.GetType(),

"keyValue",

"alert('test');",

true);

Monday, March 26, 2012

CallBack issues with AjaxControlToolKit

Greetings.

We recently encountered a problem while using the AjaxControlToolkit library. We have designed a "one-page" project, whereas none of the controls is present in the markup code at startup. At some point after the page has already been rendered we make a AJAX Call to our WebService which is responsible for programmatically creating controls, put them into a server-side page, renders the content, splits markup, javascript and reference scripts, and finally sends this three split up parts to the client through the callback.

This scenario works fine as long, as we stick to the standard webui controls or even the ComponentArt controls. But when we were trying to implement a AjaxControlToolkit control, the whole thing failed. The procedure was nearly the same when adding a normal webui control, only that we had to programmatically add a ToolkitScriptManager to our server-side created page where we throw in all the controls. We added a ToolKit control, rendered the page, split it up as usual and sent the results to the client.

The rendered markup still looked good, and the referenced scriptfiles which the client had to reinclude seemed ok, too. But the javascript rendered throws an error, which we can't tail. On one machine it throws an "AjaxControlToolkit is undefined"- javascript error, on another it throws a syntax error which is very vague.

So has anyone tried to programmatically implement the AjaxControlToolkit recently and any clues to what we seem to have missed? Help is really appreciated since we have some kind of deadline, and really wish to implement the cool features of the toolkit :)

Best regards and thanks in advance.

Hi,

Based on your description, I have a general idea of what are you trying to implement. Though, in my opinion, it's cool but may be not that efficient.

Can you create a small sample to illustrate the issue more intuitively, so that I can use it directly and try to find out the cause.