-
Feb 23rd, 2012, 04:40 AM
#1
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:
namespace ProjectName.Controls
{
public class SpecializedControl : BaseControl
{
public void SetupList()
{
dropdown.DataSource = "ABCDEFG".ToCharArray();
dropdown.DataBind();
}
}
}
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?
-
Feb 23rd, 2012, 08:23 PM
#2
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
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Feb 28th, 2012, 02:33 AM
#3
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
-
Feb 28th, 2012, 02:48 AM
#4
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...
-
Feb 29th, 2012, 01:57 AM
#5
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
-
Feb 29th, 2012, 11:35 AM
#6
Re: Registering custom / user controls problem
Hmmm that might work... I'll try that.
-
Mar 1st, 2012, 02:06 AM
#7
Re: Registering custom / user controls problem
Originally Posted by NickThissen
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|