我希望有人能帮助我。我想直接在对象上编写自定义验证代码,而不必为网站 UI 和使用相同对象的 Web 服务编写单独的方法。这是我目前拥有的代码,它可以工作,但我认为这不是最好的解决方案,特别是因为这意味着必须使用一个我真的只想使用标准字符串/整数等的对象。我曾认为也许我可以在对象的属性上使用属性,但由于我无法设置属性的值,所以这个想法就消失了。任何帮助将不胜感激。
亲切的问候,亚历克斯
<%@ Register src="TextBoxControl.ascx" tagname="TextBoxControl" tagprefix="uc1" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ValidationSummary ID = "VS" runat="server" />
Name: <uc1:TextBoxControl ID="NameTextBox" runat="server" ErrorMessage="Name is required" />
<br />Postcode: <uc1:TextBoxControl ID="PostcodeTextBox" runat="server" ErrorMessage="Postcode is required" />
<asp:Button ID="SubmitButton" runat="server" Text="Submit" OnClick="SubmitButton_OnClick" />
</asp:Content>
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SubmitButton_OnClick(object sender, EventArgs e)
{
MyObject obj = new MyObject();
obj.Name = new ValidatedString(NameTextBox.Text);
obj.Postcode = new ValidatedString(PostcodeTextBox.Text);
NameTextBox.IsValid = obj.Name.Valid;
NameTextBox.CustomError = obj.Name.GetErrors();
PostcodeTextBox.IsValid = obj.Postcode.Valid;
PostcodeTextBox.CustomError = obj.Postcode.GetErrors();
}
}
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TextBoxControl.ascx.cs" Inherits="WebApplication1.TextBoxControl" %>
<asp:TextBox ID="ValueTextBox" runat="server"></asp:TextBox>
<asp:CustomValidator ID="CV" runat="server" ControlToValidate="ValueTextBox">* </asp:CustomValidator>
<asp:RequiredFieldValidator ID="RFV" runat="server" ControlToValidate="ValueTextBox">*</asp:RequiredFieldValidator>
public partial class TextBoxControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string Text
{
get
{
return ValueTextBox.Text;
}
}
public string ErrorMessage
{
set
{
RFV.ErrorMessage = value;
}
}
public bool IsValid
{
set
{
CV.IsValid = value;
}
}
public string CustomError
{
set
{
CV.ErrorMessage = value;
}
}
}
public class MyObject
{
private ValidatedString _name = new ValidatedString();
public ValidatedString Name
{
get{
return _name;
}
set
{
if (value.Value.Length > 5)
{
value.Errors.Add("Name exceeded maximum length of 5");
value.Valid = false;
}
if (value.Value.Contains("a"))
{
value.Errors.Add("Name cannot contain the letter a");
value.Valid = false;
}
_name = value;
}
}
private ValidatedString _postcode = new ValidatedString();
public ValidatedString Postcode
{
get
{
return _postcode;
}
set
{
if (value.Value.Length > 3)
{
value.Errors.Add("Postcode exceeded maximum length of 5");
value.Valid = false;
}
if (value.Value.Contains("a"))
{
value.Errors.Add("Postcode cannot contain the letter a");
value.Valid = false;
}
_postcode = value;
}
}
}
public class ValidatedString
{
private string _value = string.Empty;
private bool _valid = true;
private List<string> _errors = new List<string>();
public ValidatedString()
{ }
public ValidatedString(string value)
{
_value = value;
}
public string Value
{
get
{
return _value;
}
set
{
_value = value;
}
}
public bool Valid
{
get
{
return _valid;
}
set
{
_valid = value;
}
}
public List<string> Errors
{
get
{
return _errors;
}
set
{
_errors = value;
}
}
public string GetErrors()
{
string returnValue = string.Empty;
foreach (string error in Errors)
{
returnValue += error + System.Environment.NewLine;
}
return returnValue;
}
}