Blog

Dynamic Controls, Ajax and ResolveUrl/ResolveClientUrl

I had this old button that besides the text renders an icon. The button control has been working for about 3 years and today I noticed it hadn’t been working for a while now. The issue I assumed is that the problem had something to do with the use of Ajax and the controls been inserted dynamically since the old pages are working fine.

After digging a little into the issue I found a solution and a tip for the probable cause. I was rendering the url of the icon inside the control with this.ResolveUrl(iconPath) which is standard procedure for ASP.Net control development. It turns out that either for being dynamic controls or being added using Ajax the control’s AppRelativeTemplateSourceDirectory isn’t correctly set.

A temporary solution is to use Page.ResolveUrl instead, because the page’s AppRelativeTemplateSourceDirectory is correctly set. Although this can be used in most cases anyone is trying to add controls dynamically this solution isn’t particularly suitable in some parts of my application (which involve controls without a real page) and iirc it might not work on the Visual Studio Designer without this complete workaround:

(Page!=null ? Page.ResolveUrl(path) : this.ResolveUrl(path))

Anyway, I’ll need to digg deep into the issue as I’m feeling this is a bug in ASP.Net or ASP.Net Ajax itself, if/when I’m sure of the exact procedure that should configure AppRelativeTemplateSourceDirectory then I might try to create a procedure to recursively fix all dynamic controls and set the correct value on them, this would be a much definitive solution as at least it would avoid having to patch any controls suffering the same behaviour.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Tags: , , ,

Leave a Reply

For spam filtering purposes, please copy the number 1078 to the field below: