.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Enhanced Required Field Validator with an Image property

Posted By:Rajshree       Posted Date: September 17, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

Required Field Validator is being used from the initial version of ASP.NET and is a part of the standard validator controls in ASP.NET 2.0. When the validator is invalid, the required field is flagged with the literal in the "Text" property. The current article proposes an additional Image property that can be used to flag the required field.
 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Design;
using System.Drawing.Design;
namespace EnhancedRequiredFieldValidaor
{
    ///
    /// Extends the required field validator with an additional ImageUrl property
    ///

    public class EnhancedValidator : RequiredFieldValidator
    {
        protected override void RegisterValidatorDeclaration()
        {
         Page.ClientScript.RegisterArrayDeclaration("Page_Validators",
            string.Format("document.getElementById(\"{0}\")",ClientID));
        }

        [EditorAttribute(typeof(ImageUrlEditor), typeof(UITypeEditor))]
        public virtual string ImageUrl
        {
            get
            {
                string imageUrl = string.Empty;
                object o = ViewState["ImageUrl"];
                if (o != null)
                    imageUrl = Convert.ToString(ViewState["ImageUrl"]);

                if (imageUrl.Contains("~/"))
                    imageUrl = imageUrl.Replace("~/", string.Empty);
                return imageUrl;
            }
            set
            {
                ViewState["ImageUrl"] = value;
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            RegisterValidatorDeclaration();
            if (HttpContext.Current != null)
            {
                Page.ClientScript.RegisterExpandoAttribute
                    (ClientID, "controltovalidate", this.ControlToValidate);
                Page.ClientScript.RegisterExpandoAttribute
                    (ClientID, "evaluationfunction", "RequiredFieldValidatorEvaluateIsValid");
                Page.ClientScript.RegisterExpandoAttribute
                    (ClientID, "initialvalue", InitialValue);
            }
            if (ImageUrl.Equals(string.Empty))
            {
                writer.Write(String.Format("{1}",
                this.ClientID, this.Text));
            }
            else
            {
                writer.Write(
                String.Format("",
                this.ClientID, this.ImageUrl));
            }
        }
    }
}





Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Assembly="EnhancedRequiredFieldValidaor" Namespace="EnhancedRequiredFieldValidaor"
    TagPrefix="cc1" %>



    Test Enhanced Required Field Validator


   
       
       
     
       
       
       
       
       
      
      
      
      
       

        
        
       

        
        
       

      
      

    
              
   



 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend