Http://dflying.dflying.net/1/archive/110_build_your_own_transformers_in_aspnet_atlas.html
ASP. NET AtlasIn (Binding) Is a powerful method to connect two objects. (You can referHttp://dflying.cnblogs.com/archive/2006/04/04/366900.htmlObtain more information about binding .)AtlasThe binding automatically applies the changed attributes of the source object to the specified attributes of the target object. But sometimes you want to modify this attribute before applying it to the target object. For example, when an indexed list is displayed, you may want the index1Starts to increase, insteadJavascriptFrom0Start. In this case, you need to useAtlas Transformer.AtlasInTransformerIt is a pipe-like thing that is inserted into the process of assigning values to the attributes of the target object by the attributes of the source object, In order to filter the attributes to be assigned./Decoration/Conversion (Here we add the source attribute1), And then assign the value to the target attribute.
AtlasProvides some built-inTransformerFor exampleAdd,Multiply,Compare. However, in actual development, we need to define our ownTransformer. Let's developCustombooleantransformerTo familiarize yourself with how to write customTransformer.
CustombooleantransformerConvert a Boolean value to a custom format. For exampleYes/NoOrCompleted/inprogress. If we choose to use binding to display a Boolean value to the user, then thisTransformerIt will be very useful, and it will bring users a more friendly user experience.
In general, createTransformerThere are four steps as follows:
- Gets the converted value passed in from the source binding object. Here we callGet_value ()Obtain the passed value and convert it to a Boolean value.
- ObtainTransformer. Here, the parameter is a string that can be divided into two parts by commas. Boolean ValueTrueWill be converted to the first part,FalseIs converted to the second part. If the input parameter is null, the default string is used.True/false.
- . In this step, you should use your own logic to convert the passed value to the value to be passed out (usually used in the previous stepTransformer). Here, we first use a comma (,) to divide the parameter into two parts, and then use the first part instead.True, Replaced by the Second PartFalse. If the parameter cannot be divided into two parts, useTrue/false.
- Outputs the converted value and calls the method.Set_value ().
Below isCustombooleantransformerOfJavascriptCode, Save itCustombooleantransformer. 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 );
}
OKNow let's test thisCustombooleantransformer. AddCheckboxAnd oneTextboxAnd bind them. WhenCheckboxSelected/When deselected,TextboxThe converted Boolean value is displayed.
Below isAspxFileHtmlDefinition. Do not forgetScriptmanagerAddCustombooleantransformer. jsFile reference.
< 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" />
Below isAtlasScript definition. SpecifyTranformerargumentIs'Yes, no'To make the Boolean ValueTrueConvertYes,FalseConvertNo.
< 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 >
Actual results in the browser: