On a given page, to let's say to display a window, I may need some JavaScript initialization, some DIVs with certain IDs and it all needs to be linked with each other. The JavaScript may need to know about a DIV's ID, the DIVs may need to be nested in a certain way and so on. Obviously I don't want to worry about this on every single page I use that code. That would make it error-prone and not DRY.
With WebForms I would solve this problem by writing a custom control. How are you guys doing this in your projects? Are you using extension methods spitting out strings, maybe user controls? What's the recommended way?
Thanks.
EDIT:
Here's an example when using master-pages:
- In the HEAD content region I would need some jQuery code that sets up some functionality
- In one content region I would put some HTML required to show a part of the window
- In a different content region I would put the actual HTML that's displayed in the window.
So all these 3 pieces would require 3 different blocks of code but would be logically linked.
EDIT 2
Code example (using a master-page)
<asp:Content ContentPlaceHolderID="HeaderContent" runat="server">
<script type="text/javascript">
$(document).ready(function() { DoSomething('div1', 'div2'); });
</script>
</asp:Content>
<asp:Content ContentPlaceHolderID="TopContent" runat="server">
<div id='div1'> ... </div>
</asp:Content>
<asp:Content ContentPlaceHolderID="BottomContent" runat="server">
<div id='div2'> ... </div>
</asp:Content>
div1 and div2 are coupled with the JavaScript function call. If I make a mistake, give the DIV a different ID, the code would break. This is a simple example that proves the point. It gets a lot more complicated when the coupling relies on div1 having a certain structure for example.