Here is a piece of my code through which i am rendering html controls with runat="server"
var sb= new StringBuilder();
sb.AppendLine(" <div class='ItemDiv'>");
sb.AppendLine(" <h2>More Products </h2>");
sb.AppendLine(" <span class='separator'></span> <ul class='ulProducts'>");
foreach (var lead in list)
{
var name = lead.ProductName;
if (name.Length > 17)
{
name = string.Format("{0}...", name.Substring(0, 17));
}
sb.AppendLine(string.Format("<li><img src='{0}' align='absmiddle' alt='{1}'/>
<span class='separator'> </span>
<a href='~/Item/{2}/{1}.aspx' runat='server' // issue is with runat='server'
style='font-size:8pt; text-transform:
capitalize;'>{1}</a></li>",
lead.ProductImagePath, name,lead.Id));
}
sb.AppendLine("</ul> </div>");
ProductsBySame1.GetHtml = sb.ToString(); // ProductsbySame is usercontrol and GetHtml is property of innerHtml of div placed in the usercontrol. That is dynamically populated.
My output:
<div id="ctl00_cphMain_ProductsBySame1_ltlProducts"> <div class='ItemDiv'>
<h2>More Products </h2>
<span class='separator'></span> <ul class='ulProducts'>
<li><img src='Uploads/Images/8e4c426aa1464af0b45f43c8c773e8ae.jpg' align='absmiddle' alt='Ceftriaxone Injec...'/> <span class='separator'> </span><a href='~/Item/659/Ceftriaxone Injec....aspx' **runat='server'** style='font-size:8pt; text-transform: capitalize;'>Ceftriaxone Injec...</a> </li>
<li><img src='Uploads/Images/9efa7b61cf9f467393089ca111fc5f51.jpg' align='absmiddle' alt='Clavulanate Potas...'/> <span class='separator'> </span><a href='~/Item/660/Clavulanate Potas....aspx' **runat='server'** style='font-size:8pt; text-transform: capitalize;'>Clavulanate Potas...</a> </li>
</ul> </div>
</div>
See ** above. Actually i am using UrlRewriting.Net module to rewrite urls. And as per their documentation the links starting with ~/ will work only. So I ambuilding my url as and since ~/ works only with runat='server'.
So finally my issue is that my above href is not working as the runat='server' is not resolved while rendering. Please help me while rendering the html , as per my scenario of usage.
Thanks in advance.
Just use a Repeater
control instead of building html from scratch:
<asp:Repeater ID="MyRepeater" runat="server">
<HeaderTemplate>
<div class="ItemDiv">
<h2>More Products </h2>
<span class='separator'></span>
<ul class='ulProducts'>
</HeaderTemplate>
<ItemTemplate>
<li>
<img src="<%...%>" align="absmiddle" alt="<%...%>" />
<span class='separator'> </span>
<a href="<%...%>" runat="server" style="font-size:8pt; text-transform:capitalize;"><%...%></a>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</div>
</FooterTemplate>
</asp:Repeater>
(added dummy placeholders for binding)