zoukankan      html  css  js  c++  java
  • Asp.net Dynamic Data之四定义字段的显示/编辑模板和自定义验证逻辑

    Asp.net Dynamic Data之四定义字段的显示/编辑模板和自定义验证逻辑

    Asp.net Dynamic data提供了一些字段模板有比如在Detail View/ListView中显示用的和在Edit View/Insert View中使用的编辑字段,同时也可以自定义你所需要的字段模板。和特殊的显示格式。

        比如:我们需要对日期类型的输出进行格式化{0:yyyy-mm-dd},对于日期的编辑我需要使用一个编辑控件供我们选择;

        一些常用的校验比如该字段是Int型并且编辑时候大小在1-100之间,显示的时候对不不同的值加于颜色的区别。

        比如我们需要对输入字段的值进行更严格的业务逻辑或是上下文的关联的验证那又要如何实现呢。

    DEMO1

    下面我们来看看如何实现这些效果最简单如何格式化显示字段内容,下面是三种不同的显示格式

    代码

    Partial Order 类是LINQ实体中定义的Order;在上面加上MetadataType的特性,下面定义一个MetadataOrder的内容,字段名需要和Order中定义的一致,然后在字段上加上显示格式的特性DisplayFormat(Dataformatstring),当然还支持多种数字类型字符窜类型;从上我们还看到有一个UIHint特性,定义显示或是编辑时使用的字段模板,比如UIHint("DevDateTime")使用自己定义的日期字段模板,Rang(1,100,ErrorMessage)这个就定义简单的验证逻辑类似ValidateControl。

    DEMO2

    自定义字段模板根据数字显示不同的颜色

    代码

    CustomFieldTemplate.ascx

    <%@ Control Language="C#" AutoEventWireup="true"

    CodeFile="CustomFieldTemplate.ascx.cs"

    Inherits="DynamicData_FieldTemplates_CustomFieldTemplate" %>

     

    <asp:Label runat="server" ID="Label1" Text="<%# FieldValueString %>" />

    CustomFieldTemplate.ascx.cs

    public partial class DynamicData_FieldTemplates_CustomFieldTemplate :

    System.Web.DynamicData.FieldTemplateUserControl {

     

    public override Control DataControl {

    get {

    return Label1;

    }

    }

     

    protected override void OnDataBinding(EventArgs e)

    {

    // Read current quantity value.

    Int16 currentQty = (Int16)FieldValue;

    if (FieldValue != null)

    {

    // Set quantity warning thresholds.

    int min = 30;

    int max = 1500;

    if (currentQty < min)

    {

    // Quantity is less than the minimum

    // warning threshold.

    Label1.ForeColor = System.Drawing.Color.Red;

    Label1.Font.Bold = true;

    }

    else

    if (currentQty > max)

    {

    // Quantity is greater than the maximum

    // warning threshold.

    Label1.ForeColor = System.Drawing.Color.Blue;

    Label1.Font.Bold = true;

    }

    }

    }

    }

     

    自定义模板继承System.Web.DynamicData.FieldTemplateUserControl 具体的我就不多说了这一块比较简单

     

    DEMO3

    输入的ShipCity必须是R开头。

     

    代码

     

    你可以针对每个字段进行验证 ,这里要注意的是VaildationException而不是Exception;

    另外你也可以根据不同操作区定义你的验证方式,比如Insert,Update,Delete

  • 相关阅读:
    基础抽象代数
    斜堆
    WC2018
    WC2019
    有向图上不相交路径计数
    生成树计数
    Pr&#252;fer序列
    反演
    1.1 Linux中的进程 --fork、孤儿进程、僵尸进程、文件共享分析
    Python程序的执行过程 解释型语言和编译型语言
  • 原文地址:https://www.cnblogs.com/neozhu/p/1302331.html
Copyright © 2011-2022 走看看