CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Eric Wise

Business & .NET

Telerik, Atlas, and my Typeable DropDownList

So I got to have some fun in the last 24 hours playing around with Atlas some more.  We're currently rebuilding the company's remote enrollment engine and we've decided that the April Atlas build is stable enough that we're going to go ahead and integrate it for a smoother client experience.

In the initial "Postback" version we were using the Telerik Radcombo because it has some nice features like being able to type into the drop down box and filtering the list.  If you've never noticed by default the dropdownlist packaged with ASP .NET only allows for filtering by a single keypress.  As such, if you have a list with:

Babbs, Bobby, Brutus

You would have to hit the "b" key three times in order to select brutus rather than typing a more intuitve "br".  I find this highly annoying hence the use of the Telerik Control.  Much to my chagrin, however, I discovered that when wrapping the telerik control in an Atlas update panel it would promptly lose its skin/css after the asynch postback.  Searched the forums, found a solution that involved hardcoding the telerik javascript include but it didn't seem to work.

Hey Scott Guthrie!  Can we get a prop on the dropdownlist control to allow typing instead of the default behavior?  =)

In the meantime, I implemented my own with some help from Jonathan Cogley.  Note that it uses .NET 2.0 features for registering client script blocks etc.  I also stripped out any case sensitivity logic since I don't want it.  Being that it inherits from dropdownlist, it plays nice with Atlas, and seems to work just fine Firefox and IE.

  • So when you type "b" it will go and find the first "b" item.
  • When you type "r" it will move to the first "br" item.
  • If there is no "br" item it will reset the attribute and move to the first "r" item.

Note that it's all case insensitive.  Without further ado, here's the control source:

    public class TypeableDropDownList : DropDownList

    {

        private static string functionName = "TypeableDropDownList_OnKeyPress";

 

        protected override void OnLoad(EventArgs e)

        {

            base.OnLoad(e);

            // define the client-side script

            StringBuilder script = new StringBuilder();

            script.Append("<script language=\"javascript\" type=\"text/javascript\"> \n");

            script.Append("  function " + functionName + "(ddl) \n");

            script.Append("  { \n");

            script.Append("    var undefined; \n");

            script.Append("    if (ddl.searchKeys == undefined) \n");

            script.Append("    { \n");

            script.Append("      ddl.searchKeys = ''; \n");

            script.Append("    } \n\n");

 

            script.Append("    var key = String.fromCharCode(window.event.keyCode); \n");

            script.Append("    ddl.searchKeys += key; \n\n");

 

            script.Append("    // convert to lowercase for compare \n");

            script.Append("    ddl.searchKeys = ddl.searchKeys.toLowerCase();\n\n");

 

            script.Append("    // loop options to find matches \n");

            script.Append("    var optionsLength = ddl.options.length; \n");

            script.Append("    for (var n=0; n < optionsLength; n++) \n");

            script.Append("    { \n");

            script.Append("      var itemText = ddl.optionsNo [N].text; \n");

            script.Append("      // option text should also be lower case \n");

            script.Append("      itemText = itemText.toLowerCase();\n");

            script.Append("      if (itemText.indexOf(ddl.searchKeys,0) == 0) \n");

            script.Append("      { \n");

            script.Append("        ddl.selectedIndex = n; \n");

            script.Append("        return false; // found it, returning false cancels Microsoft Default \n");

            script.Append("      } \n");

            script.Append("    } \n\n");

 

            script.Append("    // nothing was found, reset the search string and use the Microsoft Default \n");

            script.Append("    dropdownlist.searchKeys = key; \n");

            script.Append("    return true; \n");

            script.Append("  } \n");

            script.Append("</script>");

 

            if(!this.Page.ClientScript.IsClientScriptBlockRegistered(functionName))

            {

                this.Page.ClientScript.RegisterClientScriptBlock(Type.GetType("System.String"), functionName, script.ToString());

            }

 

            this.Attributes.Add("OnKeyPress", "return " + functionName + "(this)");

        }

    }

 



Comments

scottgu said:

Yep -- this is a common request.  The plan is to allow the number of params to be configurable on the server control (right now it is hard-coded to three).

Note that the client-side Atlas behavior does allow this property to be configured today -- so you could use that (and not the server control autocomplete) to do this with the April CTP.

Hope this helps,

Scott
# April 25, 2006 10:15 AM

Dan said:

Hi Eric,

I've been using the telerik controls for a while now and have been impressed with their AJAX offerings.  I find them easy to setup and use with very little maintenance.  Dropping pretty much any control in a callback panel "just works".  Because of this, I've found little reason to look try out Atlas.  I've read about it and it looks cool and all, but there seems to be a bit more work involved to get the same functionality as the telerik controls.

Are you using Atlas for the experience with the new technology or is there something about it that makes it better\easier to use than the telerik offerings?
# April 25, 2006 10:51 AM

Vassil Terziev said:

Eric,

If you want an Atlas-ified version of r.a.d.combo, drop us a line:) It will work fine inside the Atlas panel.

Vassil
# April 25, 2006 5:03 PM

Eric Wise said:

Thanks Vassil,

I'll poke you about that once our PO gets approved.  I used your controls at my old employer and a brief demo convinced my new employer to purchase a license as well.

Are all the controls going to be "Atlas-ified" in the next quarterly release?
# April 25, 2006 5:28 PM

Eric Wise said:

I really do like the telerik ajax stuff.  My exploration into Atlas is mostly professional curiosity at this point.  However, I will say that I tend to try to not deviate from standard Microsoft offerings very often just because of the documentation and support that is available directly from the source.

Nothing against Telerik at all, they do a damn good job of support and documentation, better than most 3rd party providers I've had experience with.  But if Microsoft offers a free plugin that does what I need and does it well I tend to stick with the source.
# April 25, 2006 9:44 PM

Eric Wise said:

Additionally I will say that the Atlas Framework in comparison to the Telerik ajax stuff as far as difficulty is a toss up.  They are both really easy and elegant.
# April 25, 2006 9:45 PM

terziev said:

Eric, all telerik controls work inside the Atlas panel. We are polishing up the implementations and the update will be available much earlier than the Q2 release.

Regarding difficulty, I think that both Atlas and our own AJAX framework are leaps and bounds ahead of other implementations and both are fairly easy to use. The good news for customers is that they won't have to make a decision whether to use Atlas or r.a.d.controls. They can use both to create stunning ASP.NET applications as the two will interoperate very nicely.
# April 26, 2006 10:28 AM

terziev said:

Quick update - we released SP2 a couple of days ago and all telerik controls are now Atals-ified:) Enjoy!
# May 3, 2006 8:23 AM

Eric Wise said:

Indeed, it certainly does behave very nicely with Atlas now!
# May 10, 2006 10:46 AM

anothercoder said:

with the new implementation coming out from telerik radAjax 1.0, when would i use atlas and when would i use the telerik controls?
# June 23, 2006 7:14 AM

Eric Wise said:

Well, we actually purchased a subscription to telerik because their controls are damn nice.  I tend to err on the side of using the controls we've purchased.

The only two downsides to using 3rd party controls in general is that you have to keep paying license fees whereas atlas is free and that a 3rd party control is a new paradigm for incoming developers to learn where atlas is the public "standard" from microsoft so the liklihood of developers already knowing it coming in is better.

That being said Telerik's controls are easy to use, so I don't see #2 as an issue with them.
# June 23, 2006 11:33 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!