实现一个自定义验证器控件[序]

昨天发布的还有点小问题,刚才抽时间进行了修改,先发布出来:

提供的外部属性:

实现代码:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Kim.AspNet.Validatator.Controls
{
    [
    AspNetHostingPermission(SecurityAction.Demand,
        Level 
= AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand,
        Level 
= AspNetHostingPermissionLevel.Minimal),
    DefaultEvent(
"ValidTextBoxChanged"),
    DefaultProperty(
"Text"),
    ToolboxData(
"<{0}:ValidTextBox runat=\"server\"> </{0}:ValidTextBox>"),
    ]
    
public class ValidTextBox : CompositeControl
    
{
        
//Contact Ways 
        private DropDownList validType;
        
//User Enter Values
        private TextBox validTextBox;
    
        
//valid user enter values
        
//can not empty
        private RequiredFieldValidator emailValidator;
        
private RequiredFieldValidator handphoneValidator;
        
private RequiredFieldValidator phoneValidator;

        
//have the right format
        private RegularExpressionValidator emilFormatValidator;
        
private RegularExpressionValidator handphoneFormatValidator;
        
private RegularExpressionValidator phoneFormatValidator;

        
private static readonly object EventValidKey =
            
new object();
       
        
// get TextBox value
       [
       Bindable(
true),
       Category(
"Appearance"),
       DefaultValue(
""),
       Description(
"get value from textbox")
       ]
        
public string TextValue
        
{
            
get
            
{
                EnsureChildControls();
                
return validTextBox.Text;
            }

            
set
            
{
                EnsureChildControls();
                validTextBox.Text 
= value;
                
            }

        }

      
        [
        Bindable(
true),
        Category(
"Appearance"),
        DefaultValue(
""),
        Description(
"Error message for the phone validator.")
        ]
        
public string RequirePhoneNumber
        
{
            
get
            
{
                EnsureChildControls();
                
return phoneValidator.ErrorMessage;
            }

            
set
            
{
                EnsureChildControls();
                phoneValidator.ErrorMessage 
= value;               
            }

        }


        [
        Bindable(
true),
        Category(
"Appearance"),
        DefaultValue(
""),
        Description(
"Error message for the phone validator.")
        ]
        
public string PhoneNumberFormat
        
{
            
get
            
{
                EnsureChildControls();
                
return phoneFormatValidator.ErrorMessage;
            }

            
set
            
{
                EnsureChildControls();
                phoneFormatValidator.ErrorMessage 
= value;              
            }

        }




        [
        Bindable(
true),
        Category(
"Appearance"),
        DefaultValue(
""),
        Description(
"Error message for the hand phone validator.")
        ]
        
public string RequireHandPhoneNumber
        
{
            
get
            
{
                EnsureChildControls();
                
return handphoneValidator.ErrorMessage;
            }

            
set
            
{
                EnsureChildControls();
                handphoneValidator.ErrorMessage 
= value;              
            }

        }


        [
       Bindable(
true),
       Category(
"Appearance"),
       DefaultValue(
""),
       Description(
"Error message for the hand phone validator.")
       ]
        
public string HandPhoneNumberFormat
        
{
            
get
            
{
                EnsureChildControls();
                
return handphoneFormatValidator.ErrorMessage;
            }

            
set
            
{
                EnsureChildControls();
                handphoneFormatValidator.ErrorMessage 
= value;
            }

        }


       
        [
        Bindable(
true),
        Category(
"Appearance"),
        DefaultValue(
""),
        Description(
"Error message for the e-mail validator.")
        ]
        
public string RequireEmailAddress
        
{
            
get
            
{
                EnsureChildControls();
                
return emailValidator.ErrorMessage;
            }

            
set
            
{
                EnsureChildControls();
                emailValidator.ErrorMessage 
= value;                
            }

        }


        [
       Bindable(
true),
       Category(
"Appearance"),
       DefaultValue(
""),
       Description(
"Error message for the e-mail validator.")
       ]
        
public string EmailAddressFormat
        
{
            
get
            
{
                EnsureChildControls();
                
return emilFormatValidator.ErrorMessage;
            }

            
set
            
{
                EnsureChildControls();
                emilFormatValidator.ErrorMessage 
= value;
            }

        }



        
// The Valid event.
        [
        Category(
"Action"),
        Description(
"Raised when the user input the value.")
        ]
        
public event EventHandler ValidTextBoxChanged
        
{
            add
            
{
                Events.AddHandler(EventValidKey, value);
            }

            remove
            
{
                Events.RemoveHandler(EventValidKey, value);
            }

        }



        
protected virtual void OnValidTextBoxChanged(EventArgs e)
        
{
            EventHandler TextBoxChangedHandler 
=
                (EventHandler)Events[EventValidKey];
            
if (TextBoxChangedHandler != null)
            
{
                TextBoxChangedHandler(
this, e);
            }

        }

       
 
        
private void _TextBox_Changed(object source, EventArgs e)
        
{
            OnValidTextBoxChanged(EventArgs.Empty);
        }


        
protected override void RecreateChildControls()
        
{
            EnsureChildControls();
        }



        
protected override void CreateChildControls()
        
{
            Controls.Clear();

            ListItem list;
            validType 
= new DropDownList();
            validType.ID 
= "validDrpList";        

            list 
= new ListItem();
            list.Text 
= "电话";
            list.Value 
= "0";
            validType.Items.Add(list);
           
            list 
= new ListItem();
            list.Text 
= "邮件";
            list.Value 
= "1";
            validType.Items.Add(list);

            list 
= new ListItem();
            list.Text 
= "手机";
            list.Value 
= "2";
            validType.Items.Add(list);

            validTextBox 
= new TextBox();
            validTextBox.ID 
= "validTextBox";
            validTextBox.Width 
= Unit.Pixel(100); 
            
//phone valid

            
            phoneValidator 
= new RequiredFieldValidator();
            phoneValidator.ID 
= "phoneValidator";
            phoneValidator.ControlToValidate 
= validTextBox.ID;
            phoneValidator.Display 
= ValidatorDisplay.Dynamic;


            phoneFormatValidator 
= new RegularExpressionValidator();
            phoneFormatValidator.ID 
= "phoneFormatValidation";         
            phoneFormatValidator.Display 
= ValidatorDisplay.Dynamic;            
            phoneFormatValidator.ControlToValidate 
= validTextBox.ID;
            phoneFormatValidator.ValidationExpression 
= @"(0\d{2}-\d{8}|0\d{3}-\d{7})-\d{2,4}|(0\d{2}-\d{8}|0\d{3}-\d{7})";
            
            
//email valid
          
           emailValidator 
= new RequiredFieldValidator();
           emailValidator.ID 
= "emailValidator";
           emailValidator.ControlToValidate 
=
               validTextBox.ID;        
           emailValidator.Display 
= ValidatorDisplay.Dynamic;

           emilFormatValidator 
= new RegularExpressionValidator();
           emilFormatValidator.ID 
= "emailFormatValidation";         
           emilFormatValidator.Display 
= ValidatorDisplay.Dynamic;            
           emilFormatValidator.ControlToValidate 
= validTextBox.ID;
           emilFormatValidator.ValidationExpression 
= @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";             
           
            
//handphone valid
      
           handphoneValidator 
= new RequiredFieldValidator();
           handphoneValidator.ID 
= "handphoneValidator";
           handphoneValidator.ControlToValidate 
=
               validTextBox.ID;        
           handphoneValidator.Display 
= ValidatorDisplay.Dynamic;

           handphoneFormatValidator 
= new RegularExpressionValidator();
           handphoneFormatValidator.ID 
= "handphoneValidation";         
           handphoneFormatValidator.Display 
= ValidatorDisplay.Dynamic;             
           handphoneFormatValidator.ControlToValidate 
= validTextBox.ID;
           handphoneFormatValidator.ValidationExpression 
= @"^1[3,5][0-9][0-9]{8}$";
               
            validTextBox.TextChanged 
+= new EventHandler(_TextBox_Changed);
            validType.AutoPostBack 
= true;

            
this.Controls.Add(validType);
            
this.Controls.Add(validTextBox);

            
this.Controls.Add(phoneValidator);
            
this.Controls.Add(phoneFormatValidator);

            
this.Controls.Add(emailValidator);
            
this.Controls.Add(emilFormatValidator);

            
this.Controls.Add(handphoneValidator);
            
this.Controls.Add(handphoneFormatValidator);
        }


        
protected override void Render(HtmlTextWriter writer)
        
{
            AddAttributesToRender(writer);
            writer.AddAttribute(
                HtmlTextWriterAttribute.Cellpadding,
                
"1"false);
            writer.RenderBeginTag(HtmlTextWriterTag.Table);

            
//phone valid
            if (validType.SelectedIndex == 0)
            
{
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);

                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                validType.RenderControl(writer);
                writer.RenderEndTag();

                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                validTextBox.RenderControl(writer);
                writer.RenderEndTag();

                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                phoneValidator.RenderControl(writer);
                writer.RenderEndTag();

                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                phoneFormatValidator.RenderControl(writer);
                writer.RenderEndTag();

                writer.RenderEndTag();
            }

            
//email valid
            if (validType.SelectedIndex == 1)
            
{
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                validType.RenderControl(writer);
                writer.RenderEndTag();

                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                validTextBox.RenderControl(writer);
                writer.RenderEndTag();

                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                emailValidator.RenderControl(writer);
                writer.RenderEndTag();

                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                emilFormatValidator.RenderControl(writer);
                writer.RenderEndTag();

                writer.RenderEndTag();
            }

            
//hand phone valid
            if (validType.SelectedIndex == 2)
            
{
                writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                validType.RenderControl(writer);
                writer.RenderEndTag();

                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                validTextBox.RenderControl(writer);
                writer.RenderEndTag();

                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                handphoneValidator.RenderControl(writer);
                writer.RenderEndTag();

                writer.RenderBeginTag(HtmlTextWriterTag.Td);
                handphoneFormatValidator.RenderControl(writer);
                writer.RenderEndTag();
                writer.RenderEndTag();
            }
                    
            writer.RenderEndTag();
        }

    }

}

效果:

posted on 2008-03-29 01:15  kim  阅读(2083)  评论(4编辑  收藏  举报

导航