none
ASP.NET User Controls used in different project don't instantiate child controls RRS feed

  • Question

  • (I have done much research on this, but either the solution does not work or it does not apply to my specific situation, so my specific situation...)

    (Also, not sure if this is the right forumn, it was the only one I saw that seemed close. Please let me know if I should repost in another forumn.)

    I have always been a middle tier guy. I have worked a lot on the front-end, but this is the first time I am being tasked with designing the whole thing from the ground up.

    First, this is what I am trying to do:

        Our Web Site solution

            -Common Project

               --UserControls - ascx files to be used throughout the Website

               --Would also house other 'global things' like CSS, templates, etc.

            -Sales web app project

                --aspx pages, resources, etc. for the sales area of the Website

            -Marketing web app project

                --aspx pages, resources, etc. for the marketing area of the Website

            -A DAL library project

               --library code to be used across the site

           -A BLL library project

           -X web app project

    My problem is that if I define a user control in Common project and then try to reference it from one of my other projects, the child controls on the user control are never instantiated.

    Here is my user control in Common project:

     <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Test.ascx.cs" Inherits="CompanyName.WebPortal.Common.UserControls.Test" %>
    <asp:TextBox runat="server" ID="txtName" />
    <asp:Button runat="server" ID="btnButton" />

    Then in my Sales project, I add a reference to my Common project. Then, in an aspx page in that project I do:

    <%@ Register Assembly="CompanyName.WebPortal.Common" Namespace="CompanyName.WebPortal.Common.UserControls" TagPrefix="MyControls" %>
    <MyControls:Test runat="server" />

    But when the test.ascx loads, my txtName and btnButton are both null.

    Doing some more research, I came across the suggestion of registering the specific ascx. But this isn't really what I want to do. I want to say "Register this dll so I can use the controls in it" not "register this control, register that control, register that other control..." Plus, from what I have seen that requires a path/url to the specific ascx file and I don’t really like that either.

    I could write custom controls and put them in a dll, but I would like to keep front-end objects accesible to the designer and my understanding is taht custom controls are code only.

    Then, doing more research I am coming to believe that the "intended way" is to not have separate projects for the logical sections of my Website, but rather have a single project and then a "user controls" folder in that. When I do testing in my Common Project, I can instantiate my user control just fine. But I would really like to not have one massive behemoth project. I know I could have a folder for Sales and a folder for Marketing, etc. and if that is what I have to do, I guess that is what I have to do. Was just trying to break it up more.

    Am I even making sense? Am I thinking about this completely wrong?

    Thanks.

    Friday, October 12, 2012 10:38 PM

All replies

  • Hallo

    Technically this should work provided you reference your 'custom control library' I also believe that you have to include the referenced binary in the web.config file so that the library is actually loaded and available to the main web application.

    Regards,

    Ian


    I.W Coetzer

    Monday, December 24, 2012 1:23 PM