In previous article, we had gone through custom Validation feature. In this article, we will look into formatting of the data present in different page and field templates. Dynamic data template provides inbuilt formatting of data based on its DB type. For example, in titles table DBType [column datatype] for price field is Money. So, by default the price value will be displayed in Money format. But sometimes, we might need to format the data as per our requirement.
Open the solution used in previous article, goto Titles.cs class and add DisplayFormat attribute to price field present in TitleMetaData as shown below:
DataFormatString property is used to change the format of displaying data. I made price should be displayed in currency format [having $ in front of the value]. Than for records which are not having price value, I am making it to display with "No Data". At the end, we are seeing the currency format should be used even while editing the price field by making ApplyFormatInEditMode = true.
Run the application, the output will be like this:
We had gone through DisplayFormat property. Now, we will look into some more properties:
ScaffoldColumn property:
This property is used to hide required columns. By passing false to it, makes a column invisible. Add the code as shown below and run the application.
Now, price column will be hidden.
ScaffoldTable property:
This property is used to hide a complete table. It will be applied at class level. By passing false to it, it will hide the table from default page.
UIHint property:
This property is used for describing custom templates, which will be used for displaying or editing the data.
Now, add UIHint property to price object with value as PriceField. Now go to FieldTemplates in solution explorer and add a new Dynamic Data Field with name as PriceField. It will create two controls, one for displaying [PriceField.ascx] and other [PriceField_Edit.ascx] is for editing.
We will format display of Price field by using PriceField.ascx. Add
<asp:TextBox runat="server" ID="txtPrice" Text="<%# FieldValueString %>" ReadOnly="true" ForeColor="Red"/> to PriceField.ascx. In code behind, replace Literal1 with txtPrice in DataControl property.
Now, run the application and the output will be like this:
In this way, we can format the display of the controls in Display or List mode.
Now, we will do formatting in Edit mode.
Go to PriceField_Edit.ascx and remove textbox control. Add a dropdown control as shown below:
Change ControlToValidate property from TextBox1 to drpPrice and make changes to code behind as shown below:
Now, run the application. We will see a dropdown control in Edit mode. In this way, we can customize the controls for displaying/editing data.
I will end up the things here. In coming articles, we will look deep into the customization of this template. I hope this will be helpful for all.