?? uploadvalidator.cs
字號(hào):
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;
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -