I found a way to make FreeTextBox work in the Edit template of an ASP.NET Ajax updatepanel control. It took me a day or so and it involves some serious hacking. Maybe this method is not completely efficient, but I thought I'd share it with you all and see if there are other ideas.
The problem with FreeTextBox / updatepanel is, the clientscript does not get registered with the page and the startup script, which runs on page load and initializes the textbox control, is never executed. As I found out, FreeTextBox uses the ASP.NET 1.x clientscript registration functions directly on the ASP.NET Page object. My workaround consists of the following steps:
- Register client script manually from code
- Override/overload the clientscript registration functions on the page and make them register the startup / the scripts with the ScriptManager control.
1. Register client script
The FreeTextBox js includes should be available on the page at all times, so here is the code to register them manually (ASP.NET 2.0 VB). I created a Page class that inherits the Web.UI.Page class and contains the following server script and derived all my pages from this custom Page class. Make sure you copy the js files from the FreeTextBox download package to your website and update the file paths in the script below.
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Page.ClientScript.RegisterClientScriptInclude("FTBFreeTextBox", VirtualPathUtility.MakeRelative(Request.Path, "~/ftb/FTB-FreeTextBox.js"))
Page.ClientScript.RegisterClientScriptInclude("FTBUtility", VirtualPathUtility.MakeRelative(Request.Path, "~/ftb/FTB-Utility.js"))
Page.ClientScript.RegisterClientScriptInclude("FTBToolbarItems", VirtualPathUtility.MakeRelative(Request.Path, "~/ftb/FTB-ToolbarItems.js"))
Page.ClientScript.RegisterClientScriptInclude("FTBPro", VirtualPathUtility.MakeRelative(Request.Path, "~/ftb/FTB-Pro.js"))
End If
End Sub
2. Override/overload the clientscript registration functions on the page
The following server code is from the same page class.
Public Overloads Sub RegisterOnSubmitStatement(ByVal key As String, ByVal script As String)
ScriptManager.RegisterOnSubmitStatement(Me, GetType(Page), key, script)
End Sub
This code makes all ASP.NET 1.x OnSubmit script registrations will be handled by the ScriptManager control. I did not check if the key is a FreeTextBox key, because I have no other controls on the page at this time using these functions. I guess one could do a little debugging and find out the keys FreeTextBox uses, so that only FreeTextBox script are redirected to the ScriptManager.
The following code is the same for Startup script. However, as FreeTextBox registers it's startup script in the window load event, it still will not work, because the load event is not raised during a partial page update. So, we have to filter the event registration from the script, which is exactly what the two Replace functions are doing.
Public Overrides Sub RegisterStartupScript(ByVal key As String, ByVal script As String)
ScriptManager.RegisterStartupScript(Me, GetType(Page), key, Replace(Replace(script, "FTB_AddEvent(window,'load',function () {", ""), "});", ""), False)
End Sub
As I mentioned before, it's not completely efficient but it's the only way I was able to make it work. I guess the problem would be efficiently solved if FreeTextBox would provide a boolean property (e.g. RegisterWithAjax), and then register with the ScriptManager if RegisterWithAjax is set to true.
Please let me know other workarounds or improvements on this code!