Wednesday, March 28, 2012

Preferred Approach to Existing .Net Server Controls and Client Scripting

In asp.net 1.1, the mechanism I used on many occassions is to add attributes to work some client side script. ie. a .NET dropdownlist:

ddlMonOfficeStart.Attributes.Add("onChange",string.Format("return ValidateTimeSelection('{0}', '{1}', 'divMonOfficeStart', 'divMonOfficeEnd')", ddlMonOfficeStart.ClientID, ddlMonOfficeEnd.ClientID));

First off, I'm not sure if the above approach is necessary with Atlas, but the next question is how to handle this in javascript:

Do I simply get the clientid of the dropdownlist and do my 'normal' javascript - or at this point do I try to 'cast' this dropdownlist object on the client side to the Sys.UI.Select object?

I see this example here:http://atlas.asp.net/docs/util/srcview.aspx?path=~/atlas/samples/controls/simple_control.src - but in this example, these are 'html' elements, not .net server side controls?

In the above attribute, the script might look like this:

<scripttype="text/javascript">

function ValidateTimeSelection(g_ddl1, g_ddl2, g_div1, g_div2) {
var dd1 = ($(g_ddl1));
...
}

</script>

in this line here: var ddl = ($(g_ddl1)) I'd rather like to do something like:

var dd1= new Sys.UI.Select($(g_ddl1));

and utilize the Atlas script library.

Hopefully this makes sense :)

You can simply continue to build your custom control with regaulr JavaScript that is rendered by the control through the Page.ClientScript.RegisterXXXScript methods. Where your 'behavior' is simple, this might still be the best approach.

However, if you wish to build a new control that makes use of the Atlas libraries and perform much more rich behavior in the client, then you can begin to develop custom controls that implement IScriptControl, that can then render the required xml-script (declarative Atlas script) to the client. You'll need of course to ensure that there is a Scriptmanager in the page, in order to render your script. This means that you can divorce yourself from writing complex JavaScript etc.

If you have bew custom behaviors that you want your custom control to make use of, then you could potentially build Atlas client-side behaviors/controls that agfain you can render through the custom control. This would also yield those client-side components for other client-side rich applications.

No comments:

Post a Comment