How best to reduce duplication across pages
I have a site that has a big form page and about half of the form will be used elsewhere, and it will be exactly the same. I want the same access to all of its elements wherever I place it, but I don't want to copy and paste the code around because that will make for too much room for error. I'd prefer to not use a WebUserControl, as I'll be using jQuery and Javasc开发者_Python百科ript inside this form and I've had rough experiences combining those with a control, while still having access to all of the elements.
I really wish the boss wasn't so bent on using ASP...PHP where are you when I need you!!?
You should use a combination of a Master Page and User Controls. There are pains in doing this - you should overcome them and become proficient at doing this. While this may be an isolated technology, those skills are certainly reusable in other areas of development (non ASP.NET) as well. You would do yourself a HUGE favor by becoming comfortable and even efficient at working with something like this.
Keep in mind that any jQuery/javascript you include on a reusable component will be included multiple times unless you change that (there are script register functions you can use to only load it once). But also keep in mind if you dynamically generate some of it based on control names, those control names will be very different than what you expect, so you need to use something like this (syntax is prolly off - I'm not testing this):
var firstName = document.getElementById('<% FirstName.ClientID %>').value;
Now the other thing to keep in mind is that line I just wrote there, while it does look for the ClientID, it's still not very reusable because it has the global variable named firstName
, and that will be a problem when I have a second user control that wants that same global variable.
There are lots of ways to continue down this path...
精彩评论