Referencing CustomValidator in Class Library

Buho1

Member
Joined
Mar 23, 2006
Messages
12
Location
Maryland, USA
Programming Experience
10+
Hey all. I've made a nice set of functions for my web applications. I've decided to make a class library out of it instead of copying the code to each project. What I found, however, is that the class library doesn't know what CustomValidator is. My first instinct was to write Imports System.Web.UI.WebControls at the top of the class, but I was shocked to find UI was missing from Web.

Google is not yielding any answers. Somebody had a similar situation to me, they couldn't find ListView, the solution was to Add Reference -> .NET -> System.Web.Extensions. I tried that and indeed I got much more under System.Web, including UI.WebControls. However, all of the classic webcontrols are still missing (CustomValidator, Textbox, Label, GridView).

Here is the type of code I'm trying to get to work:

Public Shared Function ScrubInput(ByVal Text As String, ByRef Validator As CustomValidator) As String. . .

I'm using .NET 3.5, but the same issue exists in .NET 4.0. Help appreciated!
 
I just stumbled upon the answer. The ListView mentioned above was my lead. When I tried referencing ListView after adding System.Web.Extensions, that still wasn't enough for the compiler, but VS10 recommended I add System.Web 2.0 library as a reference (weird, why not 3.5?). When I did that, CustomValidator (and all other webcontrols) is now recognized.

So in sum, to reference webcontrols in a class library:

1. Add Reference: System.Web.Extensions (3.5 or 4.0)
2. Add Reference: System.Web (2.0)
3. Add "Imports System.Web.UI.WebControls" at the top of your source
 
help page: CustomValidator Class (System.Web.UI.WebControls)
Namespace: System.Web.UI.WebControls
Assembly: System.Web (in System.Web.dll)
Class Library projects doesn't add these references automatically since it has no need for them, but in a web project you can see several .Net libraries are included by default since that project type already has a need for them. You can check references in Project properties, References page - or from Solution Explorer (show all files) in References folder.

.Net 3.5 added libraries to the base set of .Net 2.0, so VS 2005 and 2008 both uses the 2.0 version of System.Web library. Neither .Net 3.0 or .Net 3.5 added anything to ASP.Net section of .Net Framework (see the overview image here at 'Versions' .NET Framework - Wikipedia, the free encyclopedia). In .Net 4.0 all .Net libraries have been recompiled, even if many of them are unchanged from the base 2.0 libraries, so here you would normally use the 4.0 version of System.Web assembly.
 
Back
Top