Yes, you can do that sort of thing. Two ASP.NET features come to mind that might help you out here:
ASP.NET Tag Mapping. This feature lets you change the type of the object that is instantiated when the parser finds a particular ASP.NET tag (so you can, for instance, replace all HtmlAnchors with SpecialHtmlAnchors that inherit from some base class of your choice). You just need to add a tagMapping section underneath /system.web/pages/ in your web.config. (I have not tested this sample, but it should be close to correct. It's a starting point, anyway.)
<system.web> <pages> <tagMapping> <add tagType="System.Web.UI.HtmlControls.HtmlAnchor, System.Web" mappedTagType="My.Special.HtmlAnchor, MyAssembly"/> </tagMapping> </pages> </system.web>
ASP.NET Control Adapters. You can tell ASP.NET to attach an output adapter to each instantiated control of a particular type. The feature was originally intended to facilitate output adaptation for particular browsers or browser types (e.g. desktop vs. mobile), but you can do pretty much whatever you want with them. To use them, you create a
.browsers
file in yourApp_Browsers
folder (if it does not exist, right-click on your web project in VS, mouse over "Add ASP.NET Folder" and select and select "App_Browsers." Then, add your.browsers
file to this folder via the "Add New Item..." dialog. Register your custom control adapter by modifying your.browsers
file to look something like this:<browsers> <browser refID="Default"> <!-- all browsers --> <controlAdapters> <adapter controlType="System.Web.UI.HtmlControls.HtmlAnchor" adapterType="My.Custom.RolesAwareControlAdapter, MyAssembly" /> </controlAdapters> </browser> </browsers>
Control adapters inherit from
System.Web.UI.Adapters.ControlAdapter
, which has almost all of the same rendering methods that an ordinary control has. The ControlAdapter takes complete control of the rendering for the control, but can delegate rendering back to the Target control if it wishes.
Regarding adding new attributes to your controls: if you're already going to go through the existing code and add attributes, why not just change the tag type to refer to a custom control while you're at it? Public properties of custom controls get automatically promoted to markup attributes so that you can specify their values at design time.
Another good WebForms approach--although it won't fix the tedium of editing hundreds of pages and user controls--is to use a LoginView, since it's designed to solve problems like yours.
Let me know if you've got any questions; I'll be glad to clarify.