How to easily use complex HTML “templates” from designer in a library user control in ASP.NET (VB.NET)
-
22-08-2019 - |
Question
I have some HTML that I got from a designer, from witch I made a "template" in an .ascx Web User Control about like below. Now I would like to put it into a library.
How can I do this in a nice way (Without concatenating strings etc.)
(There should be more parameters)
<div style="clear:both;margin-top:50px;"></div>
<div>
<div class="title"><a href="http://<%=ArticleURL%>"><%=Title%></a></div>
<div class="picto"></div>
<div class="article"><%=Trailer%></div>
<div style="clear:both;"></div>
<div>
<ul class="comments_box">
<li><img src="site_images/picto_comments.png" alt="" width="14" height="14" border="0"></li>
<li class="comments"><a href="">22 Comments</a></li>
<li><img src="site_images/picto_arrow.png" alt="" width="15" height="16" border="0"></li>
<li class="arrow"><a href="">Share</a></li>
<li><img src="site_images/picto_photo.png" alt="" width="20" height="20" border="0"></li>
<li class="login"><a href=""><%=Nickname%></a></li>
<li class="time">59 min ago</li>
</ul>
</div>
</div>
Solution
Have you looked into Xml literals? You can create the html template like this:
Public Function GetTemplate() As String
Dim template As String = _
<string>
<div style='clear:both;margin-top:50px;'></div>
<div>
<div class='title'><a href=<%= Me.ArticleURL %>><%= Me.Title %></a></div>
<div class='picto'></div>
<div class='article'><%= Me.Trailer %></div>
<div style='clear:both;'></div>
<div>
<ul class='comments_box'>
<li><img src='site_images/picto_comments.png' alt='' width='14' height='14' border='0'/></li>
<li class='comments'><a href=''>22 Comments</a></li>
<li><img src='site_images/picto_arrow.png' alt='' width='15' height='16' border='0'/></li>
<li class='arrow'><a href=''>Share</a></li>
<li><img src='site_images/picto_photo.png' alt='' width='20' height='20' border='0'/></li>
<li class='login'><a href=''><%= Me.Nickname %></a></li>
<li class='time'>59 min ago</li>
</ul>
</div>
</div>
</string>.Value
Return template
End Function
Your "tokens" like <%= Me.ArticleURL %> can be local variables, properties, functions, etc...
For more info about xml literals and linqToXml check this sites:
http://www.ookii.org/post/xml_literals_in_visual_basic_9.aspx
http://blogs.msdn.com/bethmassi/archive/2007/10/16/getting-started-with-linq-to-xml.aspx
UPDATE: Xml literals only work in VB.NET. If you use C#, you can always have another assembly in vb where you keep all of your xml literals. Thanks for pointing it out Keltex
Hope this helps,
D.