1
我希望有人能幫助我顯示錯誤。我想直接在對象上編寫自定義驗證代碼,而不必爲網站UI和使用相同對象的Web服務編寫單獨的方法。這裏是我的代碼有目前,它的作品,但我不認爲這是最好的解決方案,特別是因爲它意味着必須使用一個對象,我真的只是想使用標準的串/ INT等。我曾想過,也許我可以在對象的屬性上使用屬性,但是因爲我無法設置創意走出窗口的屬性的值。任何幫助將受到感謝。驗證的對象,然後使用的CustomValidator
親切的問候, 亞歷
<%@ 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;
}
}