views:

162

answers:

5

I am converting a 1.1 project to 2.0 and am having an issue with some User Controls.

In the original project, there was a class library project and the website project. In the class library project, there was a folder called UserControls and in this were several ascx files and their code behind files.

I'd like to keep the same structure so I bring in these user controls into my new solution in a separate class library project. I changed it from being a full class to a partial class and changed the CodeBehind to CodeFile of the page.

In the old user control, all the web controls in the User control were listed like follows

protected System.Web.UI.WebControls.DropDownList _findModeList;

etc.

I thought with it being .net 2 and using the CodeFile attribute instead, I may not need to do this but it doesn't compile when I omit it.

However, when I include it, it does compile, but I get "Object reference not set to an instance of an object." error when trying to use _findModeList in the code file.

Is it possible to put User Controls in a class library in .net 2.0 up?

+2  A: 

Not really. The .ASCX file should be included in each Web Application project that uses it. To avoid duplicates you can store the UserControl in your Class Library's folder and use the "Add As Link" feature of Visual Studio.

Select "Add Existing Item" in your Web Application's context menu, select the user control and, instead of clicking the "Add" button, click the arrow in that button and select "Add As Link".

Julio César
+1  A: 

Are you working with a Web Application Project? Or a Web Site? It's confusing, I know...

If you are working with a WAP, use CodeFile and let ASP.NET generate the .designer.cs files for you. Do not declare the objects from your .acsx. You are declaring a new/different DropDownList when you explicitly declare _findModeList in the .cs file referenced by CodeFile. That is why you get the null reference... it has not been initialized. If you don't have a .designer file, right-click on the .ascx in Solution Explorer and click "Convert to Web Application".

If you are working with a Site rather than a WAP, use the CodeBehind attribute. It will continue to work like in ASP.NET 1.1 and compile on the fly.

To get a control to live entirely in a DLL... use "custom controls" rather than user controls. Again, confusing.

Bryan
+1  A: 

Actually you can try create new Web Application Project not Create new Web Site , because between these two are different project.

After you create Web Application just create your User Control (.ascx). After that you can try Rebuild your Web Application. After finished you can browse to your bin directory , there should be dll for your web application. Now you can use this dll in another website/web application project. Just add reference to this dll.

yose r
+1  A: 

Have you tried changing/adding the Inherits attribute in the <%@ Control %> tag?

 <%@ Control Language="C#" CodeBehind="<PathToTheFile>" Inherits="namespace.and.classname" %>
Tubbe
A: 

Thanks guys, I followed the advice generally from this thread (voted you all up) and got it working.

Graeme