Reputation: 9578
I want to have client-side validation for quick response to the user without a roundtrip to the server.
I also want the same validation for securing the code behind action on the business and data access layer.
How do you reuse that kind of code pragmatically in ASP.NET?
(note: ASP.NET, C# 3.0, .NET 3.5, Visual Studio 2008)
Upvotes: 1
Views: 912
Reputation: 36035
I don't know if there is something for regular asp.net, but you might want to check how this open source project is going about it: http://xval.codeplex.com/. Note, I haven't really used it, so I am not sure how good it is.
Upvotes: 1
Reputation: 2134
A friendly warning not to expect too much success for your efforts. This article describes quite well the fallacies in thinking about the mythical "business layer" that you are seeing in your efforts to centralize and avoid duplication of code. The upshot is that doing the kind of unification you're talking about is often too difficult to justify.
Upvotes: 0
Reputation: 20802
Build a validation project give it a web service interface so you can call with ajax, here is a tutorial, and then add it to your biz layer and your Dal layer. then all of your code can use the same validation code.
Upvotes: 0
Reputation: 994
For the UI:
You can call the method/property Page.IsValid and then, if it returns false, inspect the validators and print their error strings manually. Or just do your own error handling, whatever you see fit.
ie:
protected void OnSomeEvent(object sender, EventArgs e)
{
if (!Page.IsValid)
{
ErrorMessage.Text = "your socks are the wrong colour";
return;
}
// Continue processing.
}
Assuming a simple literal for showing an error. Obviously tailor to your own needs.
Upvotes: 0