Results 1 to 7 of 7

Thread: Registering custom / user controls problem

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Registering custom / user controls problem

    Hi,

    I have a WebUserControl, let's call it "BaseControl.ascx" that acts as a base control for a custom control (a class that inherits BaseControl). Inside BaseControl I have a couple of regular controls (for example a DropDownList called 'dropdown'), which I try to populate in the inheriting control:
    csharp Code:
    1. namespace ProjectName.Controls
    2. {
    3.     public class SpecializedControl : BaseControl
    4.     {
    5.         public void SetupList()
    6.         {
    7.             dropdown.DataSource = "ABCDEFG".ToCharArray();
    8.             dropdown.DataBind();
    9.         }
    10.     }
    11. }

    As far as I know, the only way to register a custom control (eg, not a .ascx control but a class inheriting a control) is to use this syntax on the page:
    Code:
    <%@ Register Assembly="ProjectName" Namespace="ProjectName.Controls" TagPrefix="custom" %>
    This seems to work, I can place a SpecializedControl on the page with this:
    Code:
    <custom:SpecializedControl runat="server" ID="test" />
    However, when I now call the 'SetupList' method on this control, from the Page_Load event, it seems that 'dropdown' (the DropDownList control in BaseControl.ascx) is null, it seems it is never instantiated.


    To test what is happening I just created a simple TestControl.ascx (note: an ascx file again, so a WebUserControl and not a custom control), also with a dropdown and the same SetupList method.

    When I register this control using the following syntax, everything works fine:
    Code:
    <%@ Register Src="~/Controls/TestControl.ascx" TagName="TestControl" TagPrefix="custom" %>
    I can call SetupList and the list is populated just fine.

    When I register the exact same control however with the other syntax, it does not work, and 'dropdown' is null:
    Code:
    <%@ Register Assembly="ProjectName" Namespace="ProjectName.Controls" TagPrefix="custom" %>
    In this test case I can choose which syntax to use, but in my actual case I can't. The control I want to put on my page is not an ascx file, it's a class that inherits another control (a custom control), so as far as I know I am stuck with this second syntax. So, I cannot get this to work...

    I also tried creating the control dynamically using LoadControl, but that gives the same problem.


    Long story short: it seems like I have to use the first syntax to register a control (with a 'Src' tag pointing to the control ascx file), but I can't since there is no ascx file...

    How can I do this properly?

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Registering custom / user controls problem

    Can't say that i have tried the double custom that you do but have you tried adding it to web.config directly?
    Also have a look here:
    http://haacked.com/archive/2006/11/1...eb.config.aspx

    http://weblogs.asp.net/scottgu/archi...eb-config.aspx
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Registering custom / user controls problem

    Hello,

    The only thing that I can think is that by inheriting from BaseControl, the instantiation of the controls within that UserControl is not happening.

    You could try using Page.LoadControl:

    http://msdn.microsoft.com/en-us/libr...adcontrol.aspx

    To load the BaseControl dynamically, when your control requires it, but this seems a little "messy".

    Is there a reason why you are mixing Custom Controls and UserControls? Why not simply have BaseControl as a Control in it's own right, i.e. don't use an ascx at all?

    Gary

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Registering custom / user controls problem

    sapator: tried that, didn't work, same result.

    Gep: also tried LoadControl, same result again.

    It seems to be impossible

    The reason I want it like this is because BaseControl is a generic control. It represents a filter on a particular type. It has a dropdown (amongst other controls) that shows a list of possible values for the generic type, and handles all the filtering on the selected value internally (a method that accepts a list of items and returns those that match the selected item ). Then I have one class inheriting BaseControl for each type.

    The generics are not the problem by the way, tried it without genetics too and same result.

    I let this idea go and just pit all the filtering code on the page itself. A lot more and repeated code but it works...

  5. #5
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Registering custom / user controls problem

    Hey,

    I get what you are trying to do, but why not add the logic, and controls in an actual control (i.e. render everything in code) rather than using a composite control. Dynamically render out the DropDownList, rather than placing it in an ASCX.

    Gary

  6. #6

  7. #7
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Registering custom / user controls problem

    Quote Originally Posted by NickThissen View Post
    Hmmm that might work... I'll try that.
    Fingers crossed Let me know how you get on.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width