Galin Iliev's blog

Software Architecture & Development

Text/Value pair in ASP.NET AJAX Autocomplete Extender

The scenario

I guess you are familiar with the case when user have a screen in which ClientId is needed in text box but it is quite unusual to let the users and clients to remember all those numbers. To facilitate users and clients you put DropDownList control (in case you are developing ASP.NET app) filled with ListItem elements and every item has it's Text and Value properties. Sounds common, isn't it?

The challenge

In today's world of Web 2.0  and DHTML mashups you may have same scenario like one above but needed to be implemented in similar behavior like ASP.NET AJAX Autocomplete extender does.

In other words - you have a textbox and a list with items but when user select an item instead if item's text some number or ID is put in the extended textbox.

The Solution

ASP.NET AJAX Autocomplete extendeer can be adjusted to behave that way.

First we should provide Text and Value pair... How to do that as there is strict definition of expected XML Web Service method?

ServiceMethod - The web service method to be called. The signature of this method must match the following:

public string[] GetCompletionList(string prefixText, int count) 

Note that you can replace "GetCompletionList" with a name of your choice, but the return type and parameter name and type must exactly match, including case.

There is still way. We just need to pass pair in serialized way. If you take a look at Autocomplete test page source you will see that method

   1: [System.Web.Services.WebMethod]
   2: [System.Web.Script.Services.ScriptMethod]
   3: public static string[] GetCompletionListWithContextAndValues(string prefixText, int count, string contextKey)
   4: {
   5:     System.Collections.Generic.List<string> items = new System.Collections.Generic.List<string>(GetCompletionListWithContext(prefixText, count, contextKey));
   6:        for (int i = 0; i < items.Count; i++){
   7:           items[i] = AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(items[i], i.ToString());
   8:        }
   9:            return items.ToArray();
  10: }

So if we use helper method below we can get serialized pair

AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(text, val);

But that's not all. Unfortunately we have to modify the AutoCompleteBehavior.js in order to get value instead text property. More precisely we have to add one more line in it... in function _setText - it should start like this:

_setText: function(item) {
/// <summary>
/// Method to set the selected autocomplete option on the textbox
/// </summary>
/// <param name="item" type="Sys.UI.DomElement" DomElement="true" mayBeNull="true">
/// Item to select
/// </param>
/// <returns />

var text = (item && item.firstChild) ? item.firstChild.nodeValue : null;
text = (item && item._value) ? item._value : text;

Note: The very last line is added by us. This is enough to test. Rebuild the AjaxControlToolkit source, use produced assembly instead of downloaded one and enjoy.

Hope this helps!

.NET 2.0 and 3.0 SP1

Service Pack 1 for .NET Framework 2.0 and 3.0 are released.

Microsoft .NET Framework 2.0 Service Pack 1 provides cumulative roll-up updates for customer reported issues found after the release of Microsoft .NET Framework 2.0. In addition, this release provides security improvements, and prerequisite feature support for .NET Framework 3.0 Service Pack 1, and .NET Framework 3.5.

Here is full list of fixes in .NET 2.0 SP1.

Download locations:

.NET Framework source code is LIVE for stepping into it

.NET source code was promised to be open a while ago and now this is fact. Shawn Burke posted on his blog steps to enable it.

Here are basic ones:

  1. Install the Visual Studio 2008 QFE. (If you get an error installing the Hotfix , try inserting your VS 2008 DVD and then running the Hotfix EXE again. the work in this is in progress)
  2. Start Visual Studio 2008 and bring up Tools > Options > Debugging > General.  If you are running under the Visual Basic Profile, you will need to check the box on the lower left of the Options Dialog marked "Show All Settings" before continuing (other profiles won't have this option).

    Set the following two settings:

    • Turn OFF the "Enable Just My Code" setting
    • Turn ON the "Enable Source Server Support" setting

  3. Next, bring up the "Symbols" Page and set the symbols download URL and a cache location.  Specifically, set the three settings below:

    • Set the symbol file location to be:
    • Set a cache location.  Make sure this is a location that your account has read/write access to.  A good option for this is to place this path somewhere under your user hive (e.g. d:\cache\vs2008\symbols)
    • Enable the "Search the above locations only when symbols are loaded manually" option.


(via Shawn's blog)

And this is it....Enjoy!

Read the full blog post at Shawn's blog

TechEd Video - Framework Engineering: Architecting, Designing, and Developing Reusable Libraries

As we know it is very important to write reusable code as this lower the cost of the product and decrease development time. Here is recorded nice presentation of Krzysztof Cwalina on TechEd "Framework Engineering: Architecting, Designing, and Developing Reusable Libraries".

Krzysztof is kind enough to publish slides in XPS format (1.25 MB).

Download video in WMV format (01:07:23 and 371 MB)

image image

Framework Engineering: Architecting, Designing, and Developing Reusable Libraries

This session covers the main aspects of reusable library design: API design, architecture, and general framework engineering processes. Well-designed APIs are critical to the success of reusable libraries, but there are other aspects of framework development that are equally important, yet not widely covered in literature. Organizations creating reusable libraries often struggle with the process of managing dependencies, compatibility, and other design processes so critical to the success of modern frameworks. Come to this session and learn about how Microsoft creates its frameworks. The session is based on experiences from the development of the .NET Framework and Silverlight, and will cover processes Microsoft uses in the development of managed frameworks

WeakReference ?!? Do we have strong one ?!

Have you used WeakReference class? I am sure you, probably, have in connection to GC and Memory Management in .NET (MSDN Magazine Article: Part1 and Part2). Especially if you have written complicated cache mechanisms. ASP.NET cache uses it internally.

It is very convenient because you can have the reference to some object and still allow this object to be collected by Garbage Collector (another description here) or in other words (according MSDN) "Represents a weak reference, which references an object while still allowing that object to be reclaimed by garbage collection."
Here is one example from MSDN Magazine;

   1:  void Method() {
   2:     Object o = new Object();    // Creates a strong reference to the
   3:                                 // object.
   5:     // Create a strong reference to a short WeakReference object.
   6:     // The WeakReference object tracks the Object.
   7:     WeakReference wr = new WeakReference(o);
   9:     o = null;    // Remove the strong reference to the object
  11:     o = wr.Target;
  12:     if (o == null) {
  13:        // A GC occurred and Object was reclaimed.
  14:     } else {
  15:        // a GC did not occur and we can successfully access the Object 
  16:        // using o
  17:     }
  18:  }

Here is another example how can be used.

But how does it work? Here is good explanation.

To fully understand how weak references work, let's look inside the managed heap again. The managed heap contains two internal data structures whose sole purpose is to manage weak references: the short weak reference table and the long weak reference table. These two tables simply contain pointers to objects allocated within the managed heap.
Initially, both tables are empty. When you create a WeakReference object, an object is not allocated from the managed heap. Instead, an empty slot in one of the weak reference tables is located; short weak references use the short weak reference table and long weak references use the long weak reference table.
Once an empty slot is found, the value in the slot is set to the address of the object you wish to track—the object's pointer is passed to the WeakReference's constructor. The value returned from the new operator is the address of the slot in the WeakReference table. Obviously, the two weak reference tables are not considered part of an application's roots or the garbage collector would not be able to reclaim the objects pointed to by the tables.

Hope you find it helpful.

P.S. Here are some explanations how can resurrect objects in .NET.

ADO.NET Data Services CTP is released!

ADO.NET Data Services aka Project "Astoria" December CTP is released. Mike Flasko (PM @ Astora team) posted some key points:

The following features are in this CTP:

  • Support to create ADO.NET Data Services backed by:
    • A relational database by leveraging the Entity Framework
    • Any data source (file, web service, custom store, application logic layer, etc)
  • Serialization Formats:
    • Industry standard AtomPub serialization
    • JSON serialization
  • Business Logic & Validation
    • Insert custom business/validation logic into the Request/response processing pipeline
    • simple infrastructure to build custom access policy 
  • Access Control
    • Easily control the resources viewable from a data service
  • Simple HTTP interface
    • Any platform with an HTTP stack can easily consume a data service
    • Designed to leverage HTTP semantics and infrastructure already deployed at large
  • Client libraries:
    • .NET Framework
    • Silverlight (coming soon)
  • For more information see ADO.NET Data Services official site.

    Meet Microsoft Live Labs Volta

    Here is another codename project from Microsoft that aims at Web UI experience - meet Microsoft Live Labs Volta:

    The Volta technology preview is a developer toolset that enables you to build multi-tier web applications by applying familiar techniques and patterns. First, design and build your application as a .NET client application, then assign the portions of the application to run on the server and the client tiers late in the development process. The compiler creates cross-browser JavaScript for the client tier, web services for the server tier, and communication, serialization, synchronization, security, and other boilerplate code to tie the tiers together.

    Developers can target either web browsers or the CLR as clients and Volta handles the complexities of tier-splitting for you.  Volta comprises tools such as end-to-end profiling to make architectural refactoring and optimization simple and quick. In effect, Volta offers a best-effort experience in multiple environments without any changes to the application.

    Programming model:

    In essence Volta is a recompiler. Volta works on MSIL rather than on a textual source language. Volta rewrites MSIL into any number of target languages, including, today JavaScript and MSIL itself. Rewriting, as a general technology, lets us delay permanent decisions about architecture, execution platform and browser until after our code is basically working. Furthermore, it frees us from having to express all these irreversible decisions in your source code. The result is a programming model that enables us to easily reshape a working application, and finally realizes the promise of one application running anywhere.

    Volta effects recompilation through 3 general capabilities: refactoring, retargeting, and remodulating. Refactoring converts single-tier code into distributed, concurrent code as directed by user-supplied annotations. Retargeting converts MSIL code into code for other virtual machines. Remodulating tailors a single piece of code for multiple browsers. The next 3 sections explain in more detail.

    See detailed architectural and fundamental description here.


    IIS7 Administration and Customization talk

    My talk yesterday went well and I think people found it interesting so I've decided to post slides and demo as well as some useful links

    From presentation:

    • Slides - PowerPoint 2007 - (2.4 MB)
    • Demo - VS 2008 project (961 KB)

    Some resources: