我是新的ASP.NET WebForms开发人员,我正在努力验证数据输入表单。我应该只使用服务器端验证,它必须使用纯C#。我想知道是否有一种最好的方法来验证以下形式,而不是有多个嵌套的if-else语句,这会造成混淆并使代码很难理解。
ASP.NET表单:
<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语句,例如:
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!");
}
它运作良好,但我认为它很长,它会造成混乱并使代码很难理解。 在执行操作之前,能否通过向我展示验证所有输入(或数据输入)的最佳方法来帮助我?
答案 0 :(得分:2)
您可以使用Visual Studio工具箱中的ASP.NET验证控件:
RequiredFieldValidator
CompareValidator
RangeValidator控件
RegularExpressionValidator
CustomValidator
DynamicValidator
的ValidationSummary
我的建议:RequiredFieldValidator控件,它检查输入控件是否输入了数据。您可以为要强制执行强制字段规则的每个表单元素设置RequiredFieldValidator控件。
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" Style="top: 98px;
left: 367px; position: absolute; height: 26px; width: 162px" ErrorMessage="password required"
ControlToValidate="TextBox2"></asp:RequiredFieldValidator>
另请参阅:How to use the ASP.NET Validation Control to validate the user input
答案 1 :(得分:-2)
考虑使用模型验证。 例如:
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }