c# - Create a custom ASP.NET (not MVC) directive possible? -
i'm working on advanced custom framework websites i'm developing @ work, , i'm implementing small plugin system it. want register plugins somehow in aspx file can hook different page events (load, prerender, etc.) after calling them in sequence in page_init function, example. i'll jump straight code see want do:
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %> <%@ plugin typename="myframework.plugin.core" arg1="arg1" arg2="arg2" %> <%@ plugin typename="myframework.plugin.mainmenu" arg1="arg1" arg2="arg2" %> <%@ plugin typename="myframework.plugin.ie6hacks" arg1="arg1" arg2="arg2" %> <%@ plugin typename="myframework.plugin.footer2015" arg1="arg1" arg2="arg2" %> <%@ plugin typename="myframework.plugin.sortablegridviews" arg1="arg1" arg2="arg2" %> <!doctype html> <html> <head runat="server"> <title>blah</title> </head> <body> <form id="form1" runat="server"> blah... </form> </body> </html>
see <% plugin %>
directives? don't exist in asp.net want create them. can't find on google regarding stuff. possible create custom one?
my idea once they're "registered" directives, can loop through of them (either stored in array everytime directive hit, or in page_init event) , act accordingly.
another idea had use code blocks instead:
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %> <% registerplugin("myframework.plugin.core"); %> <% registerplugin("myframework.plugin.mainmenu"); %> <% registerplugin("myframework.plugin.ie6hacks"); %> <% registerplugin("myframework.plugin.footer2015"); %> <% registerplugin("myframework.plugin.sortablegridviews"); %> <!doctype html> <html> <head runat="server"> <title>blah</title> </head> <body> <form id="form1" runat="server"> blah... </form> </body> </html>
but functions called after page events instead of running before them (or @ least before page_load) leaving me no way of detecting if have plugins registered in order act accordingly in base page's page_load events , such.
any ideas appreciated :). in advance!
the asp.net page parser doesn't support custom directives (although support custom attributes on page or masterpage directive). still can want using server controls.
let's suppose have base plugin class, this:
// derive control, plugins automatically // added page.controls collection public class mypluginbase : control { }
and example, 2 plugin classes this:
public class myplugin : mypluginbase { public string myfirstproperty { get; set; } public int mysecondproperty { get; set; } } public class myotherplugin : mypluginbase { public string myfirstproperty { get; set; } public int mysecondproperty { get; set; } }
then can write aspx webform (or ascx) this:
<%@ page ... standard attributes ... %> <plugins:myplugin runat="server" myfirstproperty="blah" mysecondproperty="1"></plugins:myplugin> <plugins:myotherplugin runat="server" myfirstproperty="blahblah" mysecondproperty="2"></plugins:myotherplugin> etc...
note work, you'll have declare plugins prefix somewhere, example in web.config, this:
<configuration> <system.web> <pages> <controls> <!-- assemblies & namespaces contain plugins classes --> <add assembly="webapplication1" namespace="webapplication1.code" tagprefix="plugins" /> <add assembly="myotherplugins" namespace="myotherplugins.plugins" tagprefix="plugins" /> </controls> </pages> </system.web> </configuration>
and in code behind class, you'll have automatic access plugins, in events including init, code this:
public partial class webform1 : page { protected void page_init(object sender, eventargs e) { // plugins on page var plugins = controls.oftype<mypluginbase>(); } }
Comments
Post a Comment