简体   繁体   English

无法使用runat =“ server”动态呈现html控件

[英]Unable to render html control with runat=“server” dynamically

Here is a piece of my code through which i am rendering html controls with runat="server" 这是我的代码的一部分,我通过这些代码使用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.

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. 实际上,我正在使用UrlRewriting.Net模块来重写url。 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'. 因此,我正在构建自己的网址,因为〜/仅适用于runat ='server'。

So finally my issue is that my above href is not working as the runat='server' is not resolved while rendering. 所以最后我的问题是我的上述href无法正常工作,因为在渲染时无法解析runat ='server'。 Please help me while rendering the html , as per my scenario of usage. 请根据我的使用情况在渲染html时帮助我。

Thanks in advance. 提前致谢。

When you add control with runat="server" from designer, then Visual Studio creates object for it in .designer.cs file. 当从设计器添加带有runat="server"控件时,Visual Studio将在.designer.cs文件中.designer.cs创建对象。 So your control is defined both in markup, and in code-behind. 因此,您的控件是在标记和代码隐藏中定义的。 If you're building your site dynamically, there is no object on Page element. 如果要动态构建网站,则Page元素上没有对象。

I would suggest you to add control to Page in CreateChildControls method. 我建议您将控件添加到CreateChildControls方法的Page中。 Something like this: 像这样:

Page.Controls.Add(new Hyperlink() { Url="http://www.example.com" });

Just use a Repeater control instead of building html from scratch: 只需使用Repeater控件,而不是从头开始构建html:

<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) (添加了用于绑定的虚拟占位符)

runat="server" is only there to tell the compiler what object to create when it compiles the aspx markup into a class. runat =“ server”仅在告诉编译器将aspx标记编译为类时要创建哪个对象。 What you're doing is setting the text property of an already-compiled control, so no additional compiling is going to occur. 您正在做的是设置已经编译的控件的text属性,因此不会进行其他编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM