Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UserControls in Class Library

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?

like image 262
Graeme Avatar asked Oct 28 '09 15:10

Graeme


1 Answers

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".

like image 196
Julio César Avatar answered Oct 17 '22 21:10

Julio César