views:

22

answers:

1

I have a hierarchy in my website project as below:

[CustomControl1 - folder]
- CustomControl1.ascx
- CustomControl1.css
- CustomControl1.js

I load css and js files dynamicaly based on which controls are used on particular page. I am doing it by using following code:

protected void Page_Load(object sender, EventArgs e)
{
    CustomControl1.AddLinks( Page.Header);
    CustomControl2.AddLinks( Page.Header);
    ...
}

where AddLinks method adds HtmlLink controls to Page.Header with href attribute set to coresponding css and/or js file.

I would like to add Interface that would force new controls to have AddLinks method but it is impossible since it is a static method. Because my custom controls inherit from Control class I cannot use abstract class and/or virtual methods either. How can I achieve my goal?

Note:

I know that similar ( about static methods in interfaces) questions was posted on SO before but I didnt found proper solution there. ( or I am too noobish to know that it was a proper solution ;-)

+1  A: 

You cant. I can see exactly why you'd like to have a static method on the interface here to implement your pattern, but c# doesnt allow static methods on interfaces, so your at a dead end here.

Assuming you dont want to just call a method on an instance of your control for some reason, your stuck in either making sure you implement all your static methods, using reflection(smelly) etc.

Why cant you just use


 myCustomControl1Instance.AddLinks(Page.Header);
 myCustomControl2Instance.AddLinks(Page.Header);

also, personally, I think the control should be smart enough to add the links if its needs them. Page.ClientScript contains methods for doing this.

jasper
I dont want to use `controlInstance.Method()` pattern because I add them in UpdatePanel without full postback, after user request any. So I know what controls might be used on page but i dont know when and where. But I will take a look on mentioned `Page.ClientScript`, thanks!
zgorawski
Bah, `Page.ClientScript` would help me to manage .js files but no .css since I cannot add them in easy, xhtml compatible way.
zgorawski