⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadvalidator.cs

📁 ASP开发网站的 关于网站的设计和说明 还有SQL的程序 数据库
💻 CS
字号:
namespace ASPNET.StarterKit.Communities {
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.ComponentModel;




    //*********************************************************************
    //
    // UploadValidator Class
    //
    // Use this control to validate HtmlInputFile controls to check
    // for uploaded files.
    //
    //*********************************************************************
	[Designer(typeof(ASPNET.StarterKit.Communities.CommunityDesigner))]	
    public class UploadValidator : Label, IValidator {


        private bool _isValid = true;
        private string _errorMessage = String.Empty;
        private string _controlToValidate = String.Empty;

     
        //*********************************************************************
        //
        // ErrorMessage Property
        //
        // The Error Message displayed when no content found.
        //
        //*********************************************************************

        public string ErrorMessage {
            get { return _errorMessage; }
            set { _errorMessage = value; }
        }


  
        //*********************************************************************
        //
        // IsValid Property
        //
        // Returns true if user passes validation.
        //
        //*********************************************************************
    
        public bool IsValid {
            get {
                return _isValid;
            }
            set {
                _isValid = value;
            }
        }



    
        //*********************************************************************
        //
        // Validate Method
        //
        // Called when validation happens. 
        //
        //*********************************************************************

        public void Validate() {
            if (!Visible || !Enabled)
                _isValid = true;
            else    
                _isValid = EvaluateIsValid();
        }




    
        //*********************************************************************
        //
        // OnInit Method
        //
        // Add this control to the page's validators collection.
        //
        //*********************************************************************

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);
            Page.Validators.Add(this);
        }



    
        //*********************************************************************
        //
        // OnUnload Method
        //
        // Remove this control from the Validators collection.
        //
        //*********************************************************************
        
        protected override void OnUnload(EventArgs e) {
            if (Page != null) {
                Page.Validators.Remove(this);
            }
            base.OnUnload(e);
        }



    
        //*********************************************************************
        //
        // EvaluateIsValid Method
        //
        // Check whether HtmlInputFile has content.
        //
        //*********************************************************************

        protected bool EvaluateIsValid() {
            // Do some validation on controlToValidate
            if (_controlToValidate == String.Empty)
                throw new Exception("You must supply a value for ControlToValidate");
        
            Control fileUpload = FindControl(_controlToValidate);
            
            if (fileUpload == null)
                throw new Exception("Can't find " + _controlToValidate);
            
            if (!(fileUpload is HtmlInputFile))
                throw new Exception("You can only validate HtmlInputFile controls");
            
			int fileSize = ((HtmlInputFile)fileUpload).PostedFile.ContentLength;
			if (fileSize==0) 
                return false;
            else
                return true;            
        }        


        //*********************************************************************
        //
        // ControlToValidate Property
        //
        // The control to validate.
        //
        //*********************************************************************

        public string ControlToValidate {
            get { return _controlToValidate; }    
            set { _controlToValidate = value; }
        }        


        //*********************************************************************
        //
        // Render Method
        //
        // Don't render if no validation error.
        //
        //*********************************************************************

        override protected void Render(HtmlTextWriter writer) {
            if (_isValid == false)
                base.Render(writer);
        }        


    
        //*********************************************************************
        //
        // UploadValidator Constructor
        //
        // Assign a default color of red to output.
        //
        //*********************************************************************

        public UploadValidator() : base() {
                
            ForeColor = System.Drawing.Color.Red;            
        }        

        

    
 
 
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -