Wednesday, March 21, 2012

Declaring controls in javascript

Is it possible to create the various toolkit controls in javascript? Specifically looking at the AutoCompleteExtender and the CollapsiblePanelExtender. My ultimate goal is to see if the controls could be used within an Atlas gadget.

Thanks,
Kyle

You can consume the .JS behavior files of the Toolkit controls directly for use outside ASP.NET (among other things), but that's not one of the primary scenarios for us, so I'm afraid we don't have much of documentation/samples for the relevant scenarios. You might find more information on this by searching the Atlas forums here and also the web as I recall someone showing how to do this with PHP or something...
Hi Kyle,

Actually you can find a straightforward example inFAQ item #13.

Thanks,
Ted

That's awesome, Ted. I got it to work with the following code, which is basically a variation of what's in the FAQ:

<script language="javascript" type="text/javascript">
function onAddBehavior( )
{
var textbox = new Sys.UI.TextBox( $('TextBoxSimpleSearchTerm') );
var watermark = new AtlasControlToolkit.TextBoxWatermark( );
watermark.set_WatermarkText( "Enter a search term" );
watermark.set_WatermarkCssClass( "watermarked" );

textbox.get_behaviors( ).add( watermark );
watermark.initialize( );
}
</script>

I do have one problem, though. I called the onAddBehavior method in the body's onload event and at that point, it looks like the AtlasControlToolkit namespace has not been defined yet because I get an error: 'AtlasControlToolkit' is undefined. When I call the method in the onclick method for a button on the page, though, it works fine.

Another way I got it to work is by extracting the javascript file for the AtlasControlToolkit.ExtenderBase object, making it an explicit javascript file and referencing that on the page.

First question: where should I call the onAddBehavior method?

Second question: Is the base control extender javascript file explicitly available anywhere or is it embedded in the Microsoft.AtlasControlExtender assembly?

My ultimate goal is to see if I can make a Gadget that incorporates toolkit extenders so my guess is that I will have to reference this javascript file directly since as far as I know, you can't include server-side controls within a Gadget. Yesno?


Hi Kyle,

Glad you were able to get it working. For your first question, there are two ways to get code to run after "Atlas" has finished loading. You can either add a handler to theSys.Application.load event or use the (little known) fact that "Atlas" will automatically call a function on your page calledpageLoad if it is defined. So in your case, I think the easiest approach would be something like:
functionpageLoad() {
OnAddBehavior();
}
Your second question is easy too. If you get thelatest release of the Toolkit, you'll see that we've taken everything that was in the extender base and made it public. It's now all found in theAtlasControlToolkit project'sExtenderBase folder.

I haven't had any experience with gadgets, but from what I understand they support "Atlas" so all our extenders that rely primarily on JavaScript behaviors should be useful.

Thanks,
Ted

Oh bother, I knew you made everything public but assumed it was in the latest CTP and when I didn't see it there, I chalked it up to a wondrous and fantastical dream. Now have the latest source and there it is in all its glory.

Having some issues with Atlas Gadgets in general but that's a separate discussion. Thanks a lot for your help, Ted.

No comments:

Post a Comment