Create a custom transformer in asp.net atlas

Source: Internet
Author: User
Tags definition net
Asp.net| Create

中文版 Version: http://dflying.dflying.net/1/archive/110_build_your_own_transformers_in_aspnet_atlas.html

asp.net binding in Atlas (binding) is a powerful way to connect two objects. (You can refer to http://dflying.cnblogs.com/archive/2006/04/04/366900.html For more information about bindings.) The atlas binding automatically applies the changed attributes on the source object to the specified properties of the target object. But sometimes you want to make some changes to this property before you apply it to the target object. For example, when you display an indexed list, you might want the index to increment from 1 instead of the default in JavaScript starting with 0. This is when you need to use the Atlas transformer. The transformer in Atlas is a pipe-like thing, it is inserted into the process of assigning the properties of the source object to the target object's properties, with a view to making the necessary filtering/decoration/conversion (where the Source property is added 1) to the property to be assigned, and then assigning to the target property.

Atlas provides some built-in transformer, such as Add,multiply,compare. However, in actual development, in most cases we need to define our own transformer. Let's develop a custombooleantransformer example to familiarize ourselves with how to write a custom transformer.

Custombooleantransformer is used to convert a Boolean value to our custom format, such as Yes/no or completed/inprogress. If we choose to use bindings to display a Boolean value to the user, then this transformer would be very useful, and it would bring a more user-friendly user experience.

In general, creating a transformer will have the following four steps:

The

Gets the value passed in from the source binding object that will be converted. Here we first call Get_value () to get the value passed in and convert it to a Boolean type. The
gets the transformer parameter. The argument here is a string that can be divided into two parts by commas (,). The Boolean value true is converted to the first part, and false is converted to the second part. If the parameter passed in is NULL, the default string True/false is substituted. The
is converted. In this step, you should convert the incoming value to the value that will be outgoing by your own logic (typically using the transformer parameter obtained in the previous step). Here we first divide the argument into two parts with a comma (,), then replace true with the first part, replacing false with the second part. If the parameter cannot be divided into two parts, then use True/false instead. The
outputs the converted value and invokes the method Set_value () to implement it.
The following is Custombooleantransformer's JavaScript code, which is saved as a 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 is 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 ';
  &NBsp;     customvalues[1] = ' false ';
   }
    var newvalue = value Customvalues[0]: customvalues[1];
   
   //Step 4, set the transformed value as output.
    Eventargs.set_value (NewValue);
}

OK, now let's test this custombooleantransformer. Add a checkbox and a TextBox to the page and bind them together. When the checkbox is checked/unchecked, the corresponding converted Boolean value is displayed in the TextBox.

The following is the HTML definition in the ASPX file. Don't forget to add a reference to the Custombooleantransformer.js file 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"/>

The following is the Atlas script definition. This specifies that tranformerargument is ' yes,no ' in order to convert the Boolean value true to Yes,false to 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>







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.