Blogs

Paul Kimmel's Blog

Defining Custom Script Properties

     

With the introduction and wide acceptance of asynchronous callbacks (generally referred to as Ajax now) I find myself writing a lot less JavaScript. The whole point of JavaScript was to make the Web experience rich like Windows chunk clients usually are. However, with Ajax (and Developer Express controls) it is now pretty easy to create a rich client experience without the apparent re-paint and roundtrip that the user experienced prior to Ajax. I still write JavaScript though because sometimes there is nothing quite like code on the client. And, sometimes when you need JavaScript you also need server values to be accessible on the client. This is where our CustomJSProperties event comes into play.

CustomJSProperties is an event that is called on every postback and callback. The CustomJSProperties event has a ASPxGridViewClientJSPropertiesEventArgs argument. This argument has a Properties property that is a Dictionary<string, object>. The string template parameter is the key and the object parameter is the value. Put any value in the Properties dictionary element and that item becomes a first class property as far as the owning object is concerned on the client. In the following example an ASPxGridView was added to a page. A SqlDataSource is connected to the Northwind Employees table. The ASPxGridView's ClientInstanceName is set to 'grid'. The ClientInstanceName provides you with any easy way to refer to Developer Express controls in script. Next, I add a CustomJSProperteis event for the grid and stored the grid's PageIndex in the property (see Listing 1). From ASP.NET and JavaScript's perspective cpCurrentPage can be accessed as grid.cpCurrentPage.

Listing 1: A demonstration of the CustomJSProperties event. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ASPxGridView1_CustomJSProperties(
      object sender,
      DevExpress.Web.ASPxGridView.ASPxGridViewClientJSPropertiesEventArgs e)
    {
      e.Properties["cpCurrentPage"] = ASPxGridView1.PageIndex;
    }
}

Tip: Use a lowercase cp prefix for ASPxGridView custom properties so as to avoid overwriting pre-existing properties. This is just a requirement added by us to prevent property collisions.

To use the value add a button that invokes a JavaScript function that uses the client name 'grid' and calls accesses the dynamic property. Here is an HTML tag that defines a button and the <script> block. The debugger statement breaks the code at that point and Figure 1 shows the dynamic cpCurrentPage property in the Quick Watch window.

Listing 2: A <Script> block and an <input> tag added to the ASP.NET to make use of the dynamically added  (through the server side CustomJSProperties event) cpCurrentPage property.

...
<script type="text/javascript" language="javascript">
  function DisplayCurrentPageIndex() {
    debugger;
    alert("Current page: " + grid.cpCurrentPage);  }
</script>
</body>

...
      <input type="button" value="Current Page Index" onclick="DisplayCurrentPageIndex()" />
    </div>
    </form>
</body>
</html>

image
Figure 1: The cpCurrentPage property added in the CustomJSProperties event looks like a first class property to the debugger and JavaScript.

Published Apr 09 2009, 09:05 PM by Paul Kimmel (DevExpress)
Bookmark and Share

Comments

 

George Malekkos said:

Very clear and informative article. Thank you!

April 10, 2009 2:03 AM
 

heather said:

Hi Paul,

As always your posts are very informative.  This is really a great idea.  Please keep this stuff flowing because this type of information isn't easy to find in your documentation.

Another element I have in all of my applications now is an internal web-service class that has methods both for synchronous and asynchronous web-service calls that can be done from the client.  I found this addition to be very powerful in driving a lot of power to the front-end via java-script but allowing me to leverage my internal infrastructure based on .net (exception logging, etc).

So I can easily see how CustomJSProperties, Javscript, Web-services combined can really take the front-end to the next level.  

One thing I have noticed over time is my collection of javascript code patterns is pretty well defined at this point.  Maybe you guys could consider publishing a "best practices" white paper that lists clear examples of where Dx would use javascript, customjsproperties for application development.

I think this would go a long way to help document some of these advanced features, aid in support questions, etc...

Also I think it would be great if you guys would share your favorite browser plug-ins that you find useful in development with Dx components.

April 10, 2009 10:56 AM
 

Paul Kimmel (DevExpress) said:

George: Simple compliments are always welcome.

April 10, 2009 2:55 PM
 

Paul Kimmel (DevExpress) said:

Heather: Keep the feedback coming. We have weekly meetings and an annual summit and we spend a lot of that time talking about customer needs and wants.

April 10, 2009 2:57 PM
 

Garth Henderson - Vanguard said:

@Paul:

My goal is to write platform biz application independent code.  How could this be used within XAF to create a "harness" for the web side with a sister harness for the win side with a common set of properties and methods?

Shouldn't our big picture DX development methodology be as similar for winforms, asp.net, WPF, SL, PDC, etc. as possible?

There will always be the extra "mile" to achieve to make a particular platform "sparkle" but the base funcitonality for 99% of biz apps should be the same.

BTW:  I really enjoy your fresh perspective on how to get the most out of existing DX functionality.  There is so much more that we can acheive with what DX has provided for us.

+1 on Heather's great point that we need to collaborate and publish "best practices" and methodologies,  We need to have an in depth conceptual wiki that allows us to develop a functional consensus to acheive rapid and stable application development.

April 12, 2009 12:01 PM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.