我希望有人可以帮助我。我想直接在对象上编写自定义验证代码,而不是为网站UI和使用相同对象的Web服务编写单独的方法。这是我目前的代码,它可以工作,但我不认为是最好的解决方案,特别是因为它意味着必须使用一个我真的只想使用标准字符串/ int等的对象。我原以为也许我可以在对象的属性上使用属性,但因为我无法设置想法超出窗口的属性的值。我们将非常感激地提供任何帮助。
亲切的问候, 亚历
<%@ 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;
}
}
答案 0 :(得分:1)
首先:分离验证器和数据。他们是两个不同的东西。它们不应合并为一个类。验证器具有验证数据的逻辑。验证是他的责任,而不是持有它正在验证的数据。另一个类,比方说你的实体,应该保存数据。
为什么?
第二:您可以进行自定义验证控制。您可以选择在客户端(javascript)或服务器端运行验证逻辑。如果您选择在服务器端运行它,您可以在库中的某个位置使用一个简单的函数,该函数将由您的代码以及自定义验证器使用。
您的邮政编码的验证码可以是:
// Validates a postcode and returning a list of errors. If the list is empty, the postcode is valid.
public List<string> ValidatePostcode(string postcode)
{
List<string> errors = new List<string>();
// Assuming postcode is not null. Otherwise, create another check.
if (postcode.Length > 3)
errors.Add("Postcode exceeded maximum length of 5");
if (postcode.Contains("a"))
errors.Add("Postcode cannot contain the letter a");
return errors;
}