Results 1 to 6 of 6

Thread: How to use Attributes for Validation

  1. #1
    Join Date
    Feb 2010
    Posts
    532

    question How to use Attributes for Validation

    Hi all,

    I am using ASP.Net in my project . It is my first time that i use Asp.net. I also use ADO.NET for database connectivity. Can anybody have any idea how to use attribute controls in ADO.Net for validation. Thanks in advance.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    How to use Attributes for Validation

    Attributes is a common language runtime allows you to add keyword-like descriptive declarations, to annotate programming elements such as types, fields, methods, and properties. Attributes are saved with the metadata of a Microsoft .NET Framework file and can be used to describe your code to the runtime or to affect application behavior at run time. While the .NET Framework supplies many useful attributes, you can also design and deploy your own. Best of luck.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    How to use Attributes for Validation

    You can use the System.ComponentModel.DescriptionAttribute to provide some information about the class:

    Code:
    Public Class Customer
        Private _nam As String
    
        Public Property Name() As String
            Get
                Ret _name
            End Get
            Set(ByVal Value As String)
                _name = Value
            End Set
        End Property
    End Class
    As you can see, the Description attribute is attached to the Customer class.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    How to use Attributes for Validation

    I will give you the example Custom Attributes.There are many attributes provided by the .NET Framework, but it gets really interesting when you build your own attributes. To do so, the Attribute class can be used to inherit from.

    Public Class ExtendeDescriptionAttribute
    Inherits System.Attribute
    Private _desc As String
    Private _displayr As Integer

    Public Sub New(ByVal description As String, ByVal displayOrder As Integer)
    MyBase.New()
    _desc = description
    _dispOrr = displayOrder
    End Sub

    Public Property Description() As String
    Get
    Ret _description
    End Get
    Set(ByVal Value As String)
    _desc= Value
    End Set
    End Property

    Public Property DispOr() As Integer
    Get
    Return _dispOr
    End Get
    Set(ByVal Value As Integer)
    _dispOr = Value
    End Set
    End Property
    End Class

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    How to use Attributes for Validation

    The usage of attributes seems to be limited. By using custom attributes you can make more generic functionality. I will give the example that a set of attributes that allows developers to easily add validation, with a minimum of code:

    Public Property Name() As String
    Get
    Return _name
    End Get
    Set(ByVal Value As String)
    _name = Value
    End Set
    End Property

    As you can see, two attributes are attached to the Name property. They will enforce two validation rules for that property: the name cannot be empty and the maximum length is 30 characters. These attributes will be the only thing you need to add to your class properties to have validation.

  6. #6
    Join Date
    Feb 2008
    Posts
    1,852

    How to use Attributes for Validation

    Working with attributes in .NET is great. In mixture with Reflection you can create broad and extensible solutions for ordinary problems very easily. As you can see in the example explain in above reply, once the base functionality is built, extending it is very easy .So you can build more custom ValidatorAttributes to carry out more validation rules, for example checking the minimum and maximum value of numeric properties, capitalization, dates etc. Best of luck.

Similar Threads

  1. Implementing attributes on a Webpage
    By Beverly Archer in forum Technology & Internet
    Replies: 5
    Last Post: 15-03-2010, 08:48 AM
  2. HTTP request attributes
    By KAIRU26 in forum Software Development
    Replies: 3
    Last Post: 24-11-2009, 05:59 PM
  3. How to get the value of AD user attributes in vbscript?
    By PARRISH in forum Active Directory
    Replies: 2
    Last Post: 21-11-2009, 12:13 AM
  4. What are Attributes in C#
    By Joel5 in forum Software Development
    Replies: 3
    Last Post: 13-11-2009, 07:31 PM
  5. LDAP attributes map??
    By Hela in forum Active Directory
    Replies: 2
    Last Post: 25-12-2008, 11:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,295,729.87205 seconds with 17 queries