<%	TypedListDefinition currentTypedList = (TypedListDefinition)_activeObject;
	bool generateGuiControls = currentTypedList.OutputSettingValues.GetRealBoolSettingValue("GenerateGuiControls", _executingGenerator.ProjectDefinition);
	if (!generateGuiControls)
		return; // DO NOT GENERATE

	string rootNamespaceToUse = _executingGenerator.RootNamespaceToUse;
	bool USING_ADAPTER = _executingGenerator.TemplateGroupToUse=="Adapter";
	Project currentProject = _executingGenerator.ProjectDefinition;
	var usableFields = currentTypedList.Fields
			.Where(f=> f.MappedFieldInstance.FieldType.RepresentedType != typeof(byte[])); // Required for DetailsForm
	XElement entityGuiInfo = SDE_GeneralUtilsGuiFiles.GetGuiInfoAsXElement(currentProject, currentTypedList.Name);
	Dictionary<string, string> actionNames = SDE_GeneralUtilsGuiFiles.GetEntityCustomActions(entityGuiInfo);
	int actionNamesCount = (actionNames.Any())
						? actionNames.Count + 1
						: 1;

	bool getDefaultGroupBy = currentTypedList.OutputSettingValues.GetRealBoolSettingValue("GetDefaultGroupBy", currentProject);
	List<Dictionary<string,string>> calculatedFields = SDE_GeneralUtilsGuiFiles.CalculatedFieldInfoList(entityGuiInfo);

	string adapterTypeTwo = (USING_ADAPTER)
						  ? "2"
						  : string.Empty;
%>using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using SD.LLBLGen.Pro.ORMSupportClasses;
using <%=rootNamespaceToUse%>;
using <%=rootNamespaceToUse%>.TypedListClasses;
<%if(_executingGenerator.TemplateGroupToUse == "Adapter")
{%>using <%=rootNamespaceToUse%>.DatabaseSpecific;
<%}%>
public partial class Controls_List<%=currentTypedList.Name%>Instances : ListControl
{
#region Page Lifecycle methods
	protected override void Page_Init(object sender, EventArgs e)
	{
		Dictionary<string, CustomAction> customActions = new Dictionary<string, CustomAction>(<%=actionNamesCount%>);

		CustomAction actionView = new View<%=currentTypedList.Name%>Action(null);
		actionView.ActionOnControls = ViewAction;
		customActions.Add(actionView.Key, actionView);

<%	if (actionNames.Any())
	{
		foreach (string thisAction in actionNames.Keys)
		{
			bool notInlineAction = string.IsNullOrEmpty(actionNames[thisAction]);
			string btnName = "null";
			if (notInlineAction)
			{
				btnName = "btn" + thisAction +"Sel";
%>		<%=btnName%>.Text = Resources.Entities.<%=currentTypedList.Name%>_<%=thisAction%> + " " + Resources.Global.ActionSelected;
<%			}
%>		CustomAction action<%=thisAction%> = new <%=thisAction%><%=currentTypedList.Name%>Action(<%=btnName%>);
		customActions.Add(action<%=thisAction%>.Key, action<%=thisAction%>);

<%		}
	}
%>		// ReSharper disable RedundantArgumentDefaultValue
		Configure(_<%=currentTypedList.Name%>DS, theGrid, null, btnExportToExcel, lblAvisos,
			 typeof(<%=currentTypedList.Name%>TypedList), false, customActions);
		// ReSharper restore RedundantArgumentDefaultValue
		ConfigureType("<%=currentTypedList.Name%>");
		lblAvisos.Visible = true;
		if (!IsPostBack)
		{ // Avoid DDBB refetch when button clicked if EnableViewState=false
			ListBasePage listPage = (ListBasePage) Page;
			listPage.ButtonsWithoutDataBind.Add(btnExportToExcel.UniqueID);
		}
	}
#endregion

	protected override string Description(object objectToProcess)
	{
		//GridViewRow row = (GridViewRow)objectToProcess;
		string description = string.Empty;
		// TODO: Complete "FieldName=" + typedList.FieldName
		<%=DotNetTemplateEngine.GetUserCodeRegion("ListDescription_" + currentTypedList.Name, "//") %>
		return description;
	}

#region Button Events methods
	/// <summary>
	/// Handles the Click event of the btnDetails control.
	/// </summary>
	/// <param name="sender">The source of the event.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void BtnDetails_Click(object sender, EventArgs e)
	{
		btnDetails.Visible = false;
		panDetails.Visible = false;
	}
#endregion

#region GridView data methods
	/// <summary>
	/// Handles the RowCommand event of the theGrid control.
	/// </summary>
    /// <param name="row"><%=currentTypedList.Name%>Row to view</param>
	protected void ViewAction(object row)
	{
		<%=currentTypedList.Name%>Row typedListRow = (<%=currentTypedList.Name%>Row)row;
<%		foreach(TypedListFieldDefinition field in usableFields)
		{

			IMappableField mappedField = field.MappedFieldInstance;

			// Set format
			// first try to load it from GuiFile
			string fieldFormat = field.OutputSettingValues.GetRealStringSettingValue("CustomFormat", currentProject);
			if(string.IsNullOrEmpty(fieldFormat))
			{	// Look for a default format
				fieldFormat = SDE_GeneralUtils.GetFormatString(Type.GetTypeCode(mappedField.FieldType.RepresentedType));
			}

			// Check if this field is the single PK field in the relatedEntity
			// If true, add a link. If not, a label with the actual value
			EntityDefinition relatedEntity = (EntityDefinition)field.MappedFieldInstance.ContainingElement;
			bool okLink = relatedEntity.OutputSettingValues.GetRealBoolSettingValue("GenerateGuiControls", currentProject);
			if (okLink)
			{
				var pkFields = SDE_GeneralUtils.GetIdentifyingFields(relatedEntity);
				okLink = (pkFields.Count() == 1) && (pkFields.First().Name == field.MappedFieldInstance.Name);
			}
			bool fieldIsString = (Type.GetTypeCode(mappedField.FieldType.RepresentedType) == TypeCode.String);
			string toString = fieldIsString
					? string.Empty
					: ".ToString()";
			string qsFormatStr = fieldIsString
					? "HttpUtility.HtmlEncode(typedListRow.{0})"
					: "typedListRow.{0}";
				// string fieldRowStr = string.Format(qsFormatStr, field.FieldAlias);
			if (okLink)
			{ // OK. We have a link. Here we can't use relatedEntity.descriptiveField as it isn't accessible at run time.
				string relatedFieldName = field.MappedFieldInstance.Name;
		%>		vlnk<%=field.FieldAlias%>.NavigateUrl = "~/ViewExisting.aspx?EntityType=" + ((int)EntityType.<%=relatedEntity.Name%>Entity).ToString() + "&<%=relatedFieldName%>=" + typedListRow.<%=field.FieldAlias%><%=toString%>;
				vlnk<%=field.FieldAlias%>.Text = <%
				if(!string.IsNullOrEmpty(fieldFormat))
				{%>String.Format("<%=fieldFormat%>", typedListRow.<%=field.FieldAlias%>);
<%				}
				else
				{%>typedListRow.<%=field.FieldAlias%><%=toString%>;
<%				}
			}
			else
			{ // No link. Just a simple label
		%>		vlbl<%=field.FieldAlias%>.Text = <%
				if(!string.IsNullOrEmpty(fieldFormat))
				{%>String.Format("<%=fieldFormat%>", typedListRow.<%=field.FieldAlias%>);
<%				}
				else
				{%>typedListRow.<%=field.FieldAlias%><%=toString%>;
<%				}
			}
		}
		foreach(Dictionary<string,string> calculatedField in calculatedFields)
		{
%>		vlbl<%=calculatedField["Name"]%>.Text = String.Format("<%=calculatedField["Format"]%>", typedListRow.<%=calculatedField["Name"] %>);
<%		}%>
		btnDetails.Visible = true;
		panDetails.Visible = true;
	}
#endregion
}
