Asp.net dynamic data-4. Display/edit template and custom validation logic of the defined fields
Asp.net dynamic data provides some field templates, such as the editing fields displayed in Detail View/listview and used in edit view/insert View, you can also customize the required field template. And special display formats.
For example, we need to format the output of the date type {0: yyyy-mm-dd}. For date editing, we need to use an editing Control for our selection;
For example, if the field is in the int type and the size is between 1 and during editing, different values are displayed in different colors.
For example, how can we implement more strict business logic or context-related verification on the values of input fields.
Demo1
Next let's take a look at how to achieve these effects the easiest way to format the display field content. Below are three different display formats
Code
The partial order class is the order defined in the LINQ object. With the metadatatype feature added above, the following defines the content of a metadataorder. The field name must be consistent with the one defined in order, then add the display format feature displayformat (dataformatstring) to the field. Of course, multiple numeric types of character escape are also supported. From the above, we can see that there is a uihint feature, defines the field template used for display or editing. For example, uihint ("devdatetime") uses its own Date Field template, Rang (1,100, errormessage) this defines a simple verification logic similar to validatecontrol.
Demo2
Custom Field templates display different colors based on numbers
Code
Customfieldtemplate. ascx
<% @ Control Language = "C #" autoeventwireup = "true"
Codefile = "customfieldtemplate. ascx. cs"
Inherits = "dynamicdata_fieldtemplates_customfieldtemplate" %>
<Asp: Label runat = "server" id = "label1" text = "<% # fieldvaluestring %>"/>
Customfieldtemplate. ascx. CS
Public partial class dynamicdata_fieldtemplates_customfieldtemplate:
System. Web. dynamicdata. fieldtemplateusercontrol {
Public override control datacontrol {
Get {
Return label1;
}
}
Protected override void ondatabinding (eventargs E)
{
// Read current quantity value.
Int16 currentqty = (int16) fieldvalue;
If (fieldvalue! = NULL)
{
// Set quantity warning thresholds.
Int min = 30;
Int max = 1500;
If (currentqty <min)
{
// Quantity is less than the minimum
// Warning threshold.
Label1.forecolor = system. Drawing. color. Red;
Label1.font. Bold = true;
}
Else
If (currentqty> MAX)
{
// Quantity is greater than the maximum
// Warning threshold.
Label1.forecolor = system. Drawing. color. blue;
Label1.font. Bold = true;
}
}
}
}
Custom template inheritanceSystem. Web. dynamicdata.FieldtemplateusercontrolI will not talk about the specifics. This is relatively simple.
Demo3
The input shipcity must start with R.
Code
You can verify each field. Note that vaildationexception is not exception;
In addition, you can also define your authentication methods based on different operation zones, such as insert, update, and delete.