Menu:

Recent Entries

Categories

Archives

Links

Blogs
- Dflying's Night
- David's Untitled Life
- Dflying's Blog in Chinese

This blog is hosted by DreamHost!

Syndicate

RSS 0.90
RSS 1.0
RSS 2.0
Atom 0.3

Build Your Own Transformers in ASP.NET Atlas

Dflying | 05 April, 2006 07:53

Binding in ASP.NET Altas is really a powerful way to connect two objects (You may find more info about binding here: http://dflying.dflying.net/1/archive/109_atlas_unleashed-bindings.html). Atlas will automatically apply the changes of the property in source object to the target object if you bind them together. But what if you want to have some modifications to the data before applying? Say if you want to show user a list with item indexes, and you want the indexes start at 1 instead of the default JavaScript start index 0. Here you should use the Atlas transformer. A transformer in Atlas is something just like a pipe connects the source binding and target binding, which filters/decorates/transforms the incoming value in some way (in this case is add 1) and then applies to the outgoing value.

Atlas provides some build in transformers such as Add, Multiply and Compare. But you may also need to create your own transformers in the real world development. Let’s see how to build your own transformers by going through a CustomBooleanTransformer demo.


The CustomBooleanTransformer is used to transform a Boolean value to your custom format, such as Yes/No or Completed/InProgress, which could be very useful when you want to display a Boolean value to user in binding.

Generally, there are four steps to create a transformer:

  1. Get the value to be transformed, which is from the source binding object. Here we get the input by get_value() method and try to parse it to Boolean if it is not.
  2. Get the parameter passed to the transformer. Here the parameter is a string which can be divided into two parts by a ‘,’. Boolean value true will be transformed to the first part while false will be transformed to the second part. If the parameter is null or empty, we just use the default Boolean value true/false instead.
  3. Do the transformation. In this section you should transform the input value in your logic. Here we firstly try to split the parameters into two parts and then use the first part to replace true and second part to replace false. If the parameter can not be splitted into two parts, we also use true/false instead.
  4. Apply the transformed value to output. Here we set the output by set_value() method.

    Here is the JavaScript code to implement CustomBooleanTransformer. Save it as CustomBooleanTransformer.js.

    Sys.BindingBase.Transformers.CustomBoolean = function(sender, eventArgs) {
        // step 1, get input value.
        var value = eventArgs.get_value();
        if (typeof(value) != 'boolean') {
            value = Boolean.parse(value);
        }
        
        // step 2, get arguments will be used in trasforming.
        var customString = eventArgs.get_transformerArgument();
        if (customString == null || customString == '') {
            customString = 'true,false';
        }
        
        // step 3, do the transformation.
        var customValues = customString.split(',');
        if (customValues.length != 2)
        {
            customValues[0] = 'true';
            customValues[1] = 'false';
        }
        var newValue = value ? customValues[0] : customValues[1];
        
        // step 4, set the transformed value as output.
        eventArgs.set_value(newValue);
    }

    Ok, let’s test our CustomBooleanTransformer. We add a checkbox and a textbox to the page and bind them together. When the checkbox gets checked/unchecked, the textbox will display the transformed binding value.

    Here is the HTML definition in ASPX. Do not forget to add a reference to our CustomBooleanTransformer.js in ScriptManager.

    <atlas:ScriptManager ID="sm1" runat="server">
        <Scripts>
            <atlas:ScriptReference Path="CustomBooleanTransformer.js" />
        </Scripts>
    </atlas:ScriptManager>
    <input id="myCheckbox" type="checkbox" />
    <input id="myTextbox" type="text" />
    

    And here is the Atlas script. We will transform the Boolean value true to Yes and false to No by setting the tranformerArgument to ‘Yes,No’.

    <page xmlns:script="http://schemas.microsoft.com/xml-script/2005">
        <references>
        </references>
        <components>
            <checkBox id="myCheckbox" />
            <textBox id="myTextBox">
                <bindings>
                    <binding dataContext="myCheckbox" dataPath="checked"  
                    property="text" transform="CustomBoolean" transformerArgument="Yes,No" />
                </bindings>
            </textBox>
        </components>
    </page>

    Take a look in browser:

Posted in Atlas. Comment: (6). Trackbacks:(379). Permalink
«Next post | Previous post»

Referers

Comments

  1. 1. tyghuijk2006  |  04/05,2006 at 16:03

    dsfsdf jjksjfusdyuushdfsjdhdf

  2. 2. tssxkaaw  |  09/22,2007 at 22:23

    [URL=http://amzhwjer.com]urcqfyhd[/URL] aejmfnuz xswxatzv http://baaamobc.com xvsxqbhg pfljbiub

  3. 3. mcttmtztya  |  03/17,2008 at 07:19

    vyrura | [url=http://zanave.com]kizupu[/url] | [link=http://vereva.com]nipepu[/link] | http://honexu.com | ryrevi | [http://syhepo.com xycuno]

  4. 4. mcttmtztya  |  03/17,2008 at 07:19

    vyrura | [url=http://zanave.com]kizupu[/url] | [link=http://vereva.com]nipepu[/link] | http://honexu.com | ryrevi | [http://syhepo.com xycuno]

  5. 5. pqdmxwqcit  |  03/23,2008 at 07:37

    Free Ringtones Free Ringtones Free Ringtones Free Ringtones Free Ringtones

  6. 6. rhromvplde  |  03/23,2008 at 17:16

    zyvaxo | [url=http://vovino.com]hiruva[/url] | [link=http://hehazu.com]vecuta[/link] | http://rarery.com | cutako | [http://pukuxe.com zyhyvi]

Leave a Reply

Auth Image