这是我的一段代码,我通过它使用 runat="server" 呈现 html 控件
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.
我的输出:
<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>
往上看。实际上我正在使用 UrlRewriting.Net 模块来重写 url。根据他们的文档,以 ~/ 开头的链接仅适用。所以我正在构建我的 url,因为 ~/ 仅适用于 runat='server'。
所以最后我的问题是我上面的 href 不起作用,因为 runat='server' 在渲染时没有解决。根据我的使用场景,请在渲染 html 时帮助我。
提前致谢。