Reputation: 15031
I worked in ASP.net MVC for the past 1.5 years. Where I used enterprise application block for server side validation. I loved the way that viewmodels bind to the view's controls and validation working in that way. But now I am working in a project which is purely webforms without MVC.
Here jQuery is used for client side validation and there is no server side validation at all. I was stressing the importance of server side validation and was planning to use enterprise library for the same.
For some reasons(may be due to the fact I worked in ASP.NET MVC recently) I am getting stuck up at a point.
In my webforms application will my validations necessarily contain the same logic for both client side and server side. Or in general what are the best practices for coding validations in ASP .net web forms?
I wanted to follow the widely accepted practise. Also are there any new concepts in for webforms validation which are better than enterprise library. samples should help me understand.
Upvotes: 2
Views: 979
Reputation: 954
You can use CustomValidators for anything, they are my favorite!
If you use HTML5 attributes like required="required"
you get client side feedback for free.
You can utilize them also to perform server side validation like so:
<asp:ValidationSummary runat="server" id="vSummary" />
<asp:TextBox runat="server" id="txtReq" required="required" />
<asp:DropDownList runat="server" id="ddlReq" required="required">
<asp:ListItem text="..." value="" />
<asp:ListItem text="Yes" value="1" />
<asp:ListItem text="No" value="0" />
</asp:DropDownList>
<asp:Button runat="server" id="cmdSubmit" text="Submit" />
Code Behind Functions:
private void buildRequiredWebControls(List<WebControl> lst, Control c)
{
if (c is WebControl)
if (String.Compare((c as WebControl).Attributes["required"] ?? String.Empty, "required", true) == 0)
lst.Add((c as WebControl));
foreach (Control ch in c.Controls)
buildRequiredWebControls(lst, ch);
}
/* --------------------------------------------- */
private Boolean addAllFieldsRequired(List<WebControl> controls)
{
foreach (WebControl w in controls)
{
if (w as TextBox != null)
if (String.IsNullOrWhiteSpace((w as TextBox).Text)) return false;
if (w as DropDownList != null)
if (String.IsNullOrWhiteSpace((w as DropDownList).SelectedValue)) return false;
}
return true;
}
/* --------------------------------------------- */
private void cmdSubmit_Click(object sender, EventArgs e)
{
vSummary.ValidationGroup = "StackOverflow";
Page.Validate("StackOverflow");
List<WebControl> requiredFields = new List<WebControl>();
this.buildRequiredWebControls(requiredFields, this);
Page.Validators.Add(new CustomValidator()
{
IsValid = this.addAllFieldsRequired(requiredFields),
ErrorMessage = "Please complete all required fields.",
ValidationGroup = "StackOverflow"
});
if (Page.IsValid)
{
//Good to Go on Required Fields
}
}
Beats the very monotonous alternative, which is to manually insert them into the html after every control:
<asp:ValidationSummary runat="server" id="vSummary" ValidationGroup="StackOverflow" />
<asp:TextBox runat="server" id="txtReq" required="required" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtReq" ErrorMessage="Please Fill Out Required Field" Text="*" ValidationGroup="StackOverflow" />
<asp:DropDownList runat="server" id="ddlReq" required="required">
<asp:ListItem text="..." value="" />
<asp:ListItem text="Yes" value="1" />
<asp:ListItem text="No" value="0" />
</asp:DropDownList>
<asp:RequiredFieldValidator runat="server" ControlToValidate="ddlReq" ErrorMessage="Please Fill Out Required Field" Text="*" ValidationGroup="StackOverflow" />
<asp:Button runat="server" id="cmdSubmit" text="Submit" />
Upvotes: 1
Reputation: 4101
I think you'd want to validate more rather than less...server-side checks in addition to client-side checks are a good idea (especially for an externally facing application). You want to be especially careful with textboxes. Remember users can shut off scripts (and totally circumvent your client-side scripts).
It's easy enough to implement server-side validation with Custom Validator controls that fire the ServerValidate event.
I haven't worked with the enterprise library, so I can't answer anything about the validation routines it provides.
Upvotes: 1