如何在ASP.NET Web表单中验证此数据输入表单? [英] How to validate this data entry form in ASP.NET Web Forms?
问题描述
我是ASP.NET WebForms的新开发人员,并且正在努力验证数据输入表单.我应该只使用服务器端验证,并且必须使用纯C#.我想知道是否有一种最佳方法来验证以下形式,而不是使用多个嵌套的if-else语句,这会使混乱并使代码很难理解.
I am new ASP.NET WebForms developer and I am struggling with validating a Data Entry Form. I should use the server-side validation only and it has to be using pure C#. I am wondering if there is a best approach to validate the following form instead of have multiple nested if-else statement which makes confusion and makes the code very difficult to understand.
ASP.NET表单:
ASP.NET Form:
<div class="form-horizontal">
<div class="form-group">
<label class="control-label col-xs-2">Type</label>
<div class="col-xs-4">
<asp:TextBox ID="txtType" runat="server"></asp:TextBox>
</div>
<label class="control-label col-xs-2">Category</label>
<div class="col-xs-4">
<asp:TextBox ID="txtCategory" runat="server"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">Item</label>
<div class="col-xs-10">
<asp:DropDownList ID="ddlItem" runat="server"></asp:DropDownList>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">Issue</label>
<div class="col-xs-10">
<asp:TextBox ID="txtIssue" runat="server"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">Suggestion</label>
<div class="col-xs-10">
<asp:TextBox ID="txtSuggestion" runat="server"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">Reference</label>
<div class="col-xs-10">
<asp:TextBox ID="txtReference" runat="server"></asp:TextBox>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-2">Priority</label>
<div class="col-xs-10">
<asp:DropDownList ID="ddlPriority" runat="server"></asp:DropDownList>
</div>
</div>
</div>
我当前验证表单的方法是使用if-else语句,例如:
My current approach of validating the form is by using if-else statements such as:
string type = string.Empty;
string category = string.Empty;
string issue = string.Empty
string suggestion = string.Empty;
string Reference = string.Empty;
if (!string.IsNullOrWhiteSpace(txtType.Text))
{
type = txtType.Text.Trim();
if (!string.IsNullOrWhiteSpace(txtCategory.Text))
{
category = txtCategory.Text.Trim();
if(!string.IsNullOrWhiteSpace(txtIssue.Text))
{
issue = txtIssue.Text.Trim();
if(!string.IsNullOrWhiteSpace(txtSuggestion.Text))
{
suggestion = txtSuggestion.Text.Trim();
try
{
//to do the operation
}
catch (Exception ex)
{
throw ex;
}
}
else
{
lblMessage.CssClass = "text-error";
lblMessage.Text = Encoder.HtmlEncode("Please fill out the Suggestion!");
}
}
else
{
lblMessage.CssClass = "text-error";
lblMessage.Text = Encoder.HtmlEncode("Please fill out the Issue!");
}
}
else
{
lblMessage.CssClass = "text-error";
lblMessage.Text = Encoder.HtmlEncode("Please fill out the Category!");
}
}
else
{
lblMessage.CssClass = "text-error";
lblMessage.Text = Encoder.HtmlEncode("Please fill out the Type!");
}
它可以很好地工作,但是我认为它很长,并且会造成混乱,并使代码很难理解.在操作之前,请向我展示验证所有输入(或数据输入)的最佳正确方法,是否可以帮助我?
It works well but I think it is lengthy and it makes confusion and makes the code very difficult to understand. Could you please help me by showing me the best and right approach to validate all the inputs (or data entry) before doing the operation?
推荐答案
您可以从Visual Studio工具箱中使用ASP.NET验证控件:
You can use the ASP.NET validation controls from the Visual Studio toolbox:
RequiredFieldValidator
RequiredFieldValidator
CompareValidator
CompareValidator
RangeValidator
RangeValidator
RegularExpressionValidator
RegularExpressionValidator
CustomValidator
CustomValidator
DynamicValidator
DynamicValidator
ValidationSummary
ValidationSummary
My recommendation: the RequiredFieldValidator Control, which checks to see if the data is entered for the input control. You can have a RequiredFieldValidator control for each form element on which you wish to enforce the Mandatory Field rule.
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" Style="top: 98px;
left: 367px; position: absolute; height: 26px; width: 162px" ErrorMessage="password required"
ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
另请参阅: 查看全文