Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Using Template Fields in User Controls

Dynamics AX 2009

The data in AxBoundField components in User Controls is controlled by the Enterprise Portal framework and ASP.NET. You may want to have code that interacts with or controls this bound field data. You can use a template field to do this.

You use the Dynamics AX Bound Field Designer to add bound fields to a User Control. When you view the bound field in the ASP.NET markup, it resembles the following example:

<dynamics:AxBoundField DataField="calculateDistance**" DataSet="FMTripList" 
    DataSetView="FMTrips" HeaderText="Distance">
</dynamics:AxBoundField>

In the Dynamics AX Bound Field Designer, you can select the bound field and then click Convert to Template Field. This will change the ASP.NET markup and convert the bound field to a template field. The following example shows the ASP.NET markup after the bound field in the previous example was converted to a template field:

<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Distance">
    <EditItemTemplate>
        <asp:TextBox ID="TextBox1" runat="server" 
            Columns="<%$ AxDataSet:FMTripList.FMTrips.calculateDistance**.DisplayLength %>" 
            Enabled="<%$ AxDataSet:FMTripList.FMTrips.calculateDistance**.AllowEdit %>" 
            MaxLength="<%$ AxDataSet:FMTripList.FMTrips.calculateDistance**.StringSize %>" 
            Text='<%# Bind("[calculateDistance**]") %>'></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" 
            Text='<%# Bind("[calculateDistance**]") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

The template field will still function like the bound field. However, the ASP.NET markup for the field has been split into two sections. The EditItemTemplate section contains the code that is run when the template field is shown in edit mode. The ItemTemplate section contains the code that is run when the template field is shown in view-only mode. You can use standard ASP.NET code to perform actions based on the value of the template field.

The following example shows how an AxBoundField can be converted to a template field so additional code can be added for it. In this example, the calculatedDistance bound field is part of the TripList User Control. When the trip distance is larger than or equal to 100, a red circle will be displayed next to the distance value. The TripList User Control is shown in the following illustration.

User Control with Template Field

The Dynamics AX Bound Field Designer was used to convert the calculateDistance bound field into a template field. In the ItemTemplate section of the template field, the following ASP.NET markup code was added:

<asp:Image runat="server" src="/_layouts/ep/images/Red.gif" 
    Visible='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem,"calculateDistance**")) >= 100 ? true : false %>'></asp:Image>

This code references the Red.gif image, which is deployed with Enterprise Portal. The code examines the value of the calculateDistance field. If it is larger than or equal to 100, the image is displayed. The following example shows all of the ASP.NET code for the template field:

<asp:TemplateField ConvertEmptyStringToNull="False" HeaderText="Distance">
    <EditItemTemplate>
        <asp:TextBox ID="TextBox1" runat="server" 
            Columns="<%$ AxDataSet:FMTripList.FMTrips.calculateDistance**.DisplayLength %>" 
            Enabled="<%$ AxDataSet:FMTripList.FMTrips.calculateDistance**.AllowEdit %>" 
            MaxLength="<%$ AxDataSet:FMTripList.FMTrips.calculateDistance**.StringSize %>" 
            Text='<%# Bind("[calculateDistance**]") %>'></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="Label11" runat="server" 
            Text='<%# Bind("[calculateDistance**]") %>'></asp:Label>
        <asp:Image runat="server" src="/_layouts/ep/images/Red.gif" Visible='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem,"calculateDistance**")) >= 100 ? true : false %>'></asp:Image>
    </ItemTemplate>
</asp:TemplateField>
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.