Using custom controls from App_Code folder

If you are developing a web application, and when are trying to add a Class File to the project Visual Studio will ask a confirmation like “You are attempting to add a special file type (class) to an ASP.NET Web site. In general, to use this type of item in your site, you should place it in the ‘App_Code’ folder. Do you want to place the file in the ‘App_Code’ folder?” – If you say yes, it will create(if not exists) an App_Code folder and place the class file in this folder. The App_Code is an ASP.Net special folder introduced in ASP.Net 2.0 onwards, it acts as the BIN folder, where you can store source files, which will be compiled in runtime. You can find more details about App_Code folder in MSDN : Shared Code Folders in ASP.NET Web Sites.

Few days back I created a custom contol in App_Code folder, and I couldn’t found any way to add in my aspx page. After doing some search I found two ways to do it.

  1. Directly specifying App_code folder in ASPX Page.
  2. <%@ Register TagPrefix="dotnetthoughts" Assembly="__Code" Namespace="dotnetthoughts.Controls" %>
    

    In this Assembly attribute specifies the control is from App_Code folder. And the Namespace specifies the Control name space.(By default when you are adding a Class file namespace won’t be there, you need to add a Namespace.)

  3. Registering the Control to application via Web.Config.
  4. <configuration>
            <system.web>
                    <pages>
                            <controls>
                                    <add namespace="dotnetthoughts.Controls" tagPrefix="dotnetthoughts"/>
                            </controls>
                    </pages>
            </system.web>
    </configuration>
    

    In this you don’t need to specify the control is loading from App_Code folder. Hopes it will be useful.

No related content found.

This entry was posted in .Net, .Net 3.0 / 3.5, ASP.Net and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>