Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make custom Dynamic Data field template combo box required
    primarykey
    data
    text
    <p>I have created a custom dynamic data field template to display a drop down list of states. The drop down appears no problem. But it is not displaying as 'Required'. Meaning if I leave it blank, I can still move on to the next page. Other fields on the page do validate correctly.</p> <p>HTML of the field template</p> <pre><code>&lt;%@ Control Language="VB" AutoEventWireup="false" CodeFile="State_Edit.ascx.vb" Inherits="State_EditField" %&gt; &lt;asp:ObjectDataSource ID="objectDataSourceState" runat="server" SelectMethod="GetAll" DataObjectTypeName="AsbestosEntities.lu_state" TypeName="AsbestosBusiness.StateController"&gt; &lt;/asp:ObjectDataSource&gt; &lt;asp:DropDownList ID="DropDownList1" runat="server" CssClass="ui-corner-all ui-widget input-text" AppendDataBoundItems="true" DataSourceID="objectDataSourceState" DataTextField="description" DataValueField="code"&gt; &lt;asp:ListItem Value="-1" Text=""&gt;&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" CssClass="validator" ControlToValidate="DropDownList1" Display="Static" Enabled="false" InitialValue="-1" /&gt; &lt;asp:DynamicValidator runat="server" ID="DynamicValidator1" CssClass="validator" ControlToValidate="DropDownList1" Display="Static" /&gt; </code></pre> <p>Code Behind of the field template</p> <pre><code>Imports System.ComponentModel.DataAnnotations Imports System.Web.DynamicData Imports System.Web Class State_EditField Inherits FieldTemplateUserControl Public Overrides ReadOnly Property DataControl As Control Get Return DropDownList1 End Get End Property Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If (DropDownList1.Items.Count = 0) Then If Mode = DataBoundControlMode.Insert OrElse Not Column.IsRequired Then DropDownList1.Items.Add(New ListItem("[Not Set]", "")) End If PopulateListControl(DropDownList1) End If SetUpValidator(RequiredFieldValidator1) SetUpValidator(DynamicValidator1) End Sub Protected Overrides Sub OnDataBinding(ByVal e As EventArgs) MyBase.OnDataBinding(e) Dim selectedValueString As String = GetSelectedValueString() Dim item As ListItem = DropDownList1.Items.FindByValue(selectedValueString) If item IsNot Nothing Then DropDownList1.SelectedValue = selectedValueString End If End Sub Protected Overrides Sub ExtractValues(ByVal dictionary As IOrderedDictionary) ' If it's an empty string, change it to null Dim value As String = DropDownList1.SelectedValue If String.IsNullOrEmpty(value) Then value = Nothing End If ExtractForeignKey(dictionary, value) End Sub End Class </code></pre> <p>The form view to display the field</p> <pre><code>&lt;asp:FormView ID="FacilityInfo" runat="server" AllowPaging="True" DefaultMode="Insert" DataKeyNames="facility_id"&gt; &lt;InsertItemTemplate&gt; &lt;p&gt; &lt;h2&gt; Enter the facility information:&lt;/h2&gt; &lt;/p&gt; &lt;p&gt; &lt;asp:Label ID="Label7" runat="server" Text="State" CssClass="detailheader"&gt;&lt;/asp:Label&gt;&lt;br /&gt; &lt;asp:DynamicControl ID="facility_state" runat="server" DataField="facility_state" HtmlEncode="true" Mode="Insert" CssClass="detailitem" /&gt; &lt;/p&gt; &lt;/InsertItemTemplate&gt; &lt;/asp:FormView&gt; </code></pre> <p>The model</p> <pre><code>Imports System.ComponentModel Imports System.ComponentModel.DataAnnotations &lt;MetadataType(GetType(facilityMetadata))&gt; _ Public Class facility &lt;Key()&gt; _ Public Property facility_id As Integer Public Property facility_state As String End Class Public Class facilityMetadata &lt;UIHint("LongText")&gt; _ &lt;DisplayName("Facility ID")&gt; _ &lt;Required(ErrorMessage:="Facility ID is required")&gt; _ Public Property facility_id As Integer &lt;UIHint("State")&gt; _ &lt;DisplayName("State")&gt; _ &lt;StringLength(2, ErrorMessage:="State cannot be more than 2 characters")&gt; _ &lt;Required(ErrorMessage:="Facility state is required")&gt; _ &lt;RegularExpression("^[a-zA-Z]+$", ErrorMessage:="State can only contain letters.")&gt; _ Public Property facility_state As String End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload