【问题标题】:How to make custom Dynamic Data field template combo box required如何使自定义动态数据字段模板组合框需要
【发布时间】:2013-04-03 14:53:42
【问题描述】:

我创建了一个自定义动态数据字段模板来显示状态下拉列表。下拉似乎没有问题。但它没有显示为“必需”。意思是如果我把它留空,我仍然可以进入下一页。页面上的其他字段确实验证正确。

字段模板的 HTML

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="State_Edit.ascx.vb"       Inherits="State_EditField" %>

<asp:ObjectDataSource ID="objectDataSourceState" runat="server" SelectMethod="GetAll" DataObjectTypeName="AsbestosEntities.lu_state" 
TypeName="AsbestosBusiness.StateController">
</asp:ObjectDataSource>
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="ui-corner-all ui-widget input-text" AppendDataBoundItems="true" DataSourceID="objectDataSourceState" DataTextField="description" DataValueField="code">
<asp:ListItem Value="-1" Text=""></asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1" CssClass="validator" ControlToValidate="DropDownList1" Display="Static" Enabled="false" InitialValue="-1" />
<asp:DynamicValidator runat="server" ID="DynamicValidator1" CssClass="validator" ControlToValidate="DropDownList1" Display="Static" />

字段模板的代码

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

显示字段的表单视图

<asp:FormView ID="FacilityInfo" runat="server" AllowPaging="True" DefaultMode="Insert"
                                DataKeyNames="facility_id">
    <InsertItemTemplate>
        <p>
            <h2>
                Enter the facility information:</h2>
        </p>
        <p>
            <asp:Label ID="Label7" runat="server" Text="State" CssClass="detailheader"></asp:Label><br />
            <asp:DynamicControl ID="facility_state" runat="server" DataField="facility_state" HtmlEncode="true"
                                            Mode="Insert" CssClass="detailitem" />                                            
        </p>
    </InsertItemTemplate>
</asp:FormView>

型号

Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations

<MetadataType(GetType(facilityMetadata))> _
Public Class facility

    <Key()> _
    Public Property facility_id As Integer
    Public Property facility_state As String

End Class

Public Class facilityMetadata

    <UIHint("LongText")> _
    <DisplayName("Facility ID")> _
    <Required(ErrorMessage:="Facility ID is required")> _
    Public Property facility_id As Integer
    <UIHint("State")> _
    <DisplayName("State")> _
    <StringLength(2, ErrorMessage:="State cannot be more than 2 characters")> _
    <Required(ErrorMessage:="Facility state is required")> _
    <RegularExpression("^[a-zA-Z]+$", ErrorMessage:="State can only contain letters.")> _
    Public Property facility_state As String

End Class

【问题讨论】:

    标签: asp.net entity-framework webforms asp.net-dynamic-data


    【解决方案1】:

    我认为问题是

    <asp:ListItem Value="-1" Text=""></asp:ListItem>
    

    位于您的 DropDownList 中。

    尝试使用

    <asp:ListItem Value="" Text=""></asp:ListItem>
    

    而不是你正在使用。

    编辑:

    我刚刚模拟了您的问题,发现您正在使用自定义字段模板的 AutoEventWireup 页面属性

    AutoEventWireup="false"
    

    防止您的 Page_Load 事件自动挂接到页面的 Load 事件。

    将此Page属性的值更改为true(或删除一个,因为默认值为true),然后重试。

    一些信息:

    What does AutoEventWireUp page property mean?

    【讨论】:

    • 太棒了。谢谢。有趣的是,我注意到 Visual Studio 创建的动态字段模板没有明确设置,只是没有列出,这意味着默认为 true。但是,当我创建用户控件并将其转换为字段模板时,AutoEventWireup 设置为 false。因此,作为一个实验,我将 AutoEventWireup 设置为 false,然后显式处理 Page_Load 事件(添加 Handles Me.Load)——页面加载是设置验证器的地方——这也有效。
    猜你喜欢
    • 1970-01-01
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多