I have bunch of HTML code I am using to make rounded edge boxes on my controls. Is there a way to take this code and turn it into some kind of control or something so I do not have to keep pasting 10 lines of HTML code around everything I do?
<div id="BottomBody">
<div class="box1024" >
<div class="content1024">
<div class="top1024"></div>
<h1>My Header Information</h1>
<hr />
<p>Some text for everyone!</p>
</div>
<div class="bottom1024">
<div></div>
</div>
</div>
</div>
One additional thing to note, the number HTML tags used inside the inner most DIV will change depending on where I use it in my site. So in some cases I will only have 1 tag and 1
tag but in other cases I could have 1 tag, 1
tag, 3 tags, and a HTML table. How can I make that work?
Yes, you’re thinking of a UserControl. Extract the relevant HTML out, paste it into a UserControl .ascx template.
Now in your case, you’ll probably want the text to be customizable, am I right? So you’ll need to replace the
<h1>through</p>bit with an ASP.NET label. The resulting .ascx HTML (not counting the@Controldirective) will look something like:Alternatively, you could do two labels — one for the header, one for the main text. Or even just have the header be
runat="Server"itself.Next, you’ll write a little bit of code in the .ascx code-behind file to expose the label’s (or labels’, as the case may be)
Textproperty. This would probably look something like:If you’re in an ASP.NET MVC world, use your
Modeldata instead of a label, and pass in the desired display textstringas the model data.Edit
Addressing the update to the question:
The exact same technique, assuming that the content you’re referring to is what’s within
<div class="content1024">. TheTextproperty of a label can contain any desired arbitrary HTML, and of course you can pass any arbitrary amount of HTML as a string to the Model if you’re using MVC.