문제

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)

다른 팁

When you add control with runat="server" from designer, then Visual Studio creates object for it in .designer.cs file. 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.

I would suggest you to add control to Page in CreateChildControls method. Something like this:

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

runat="server" is only there to tell the compiler what object to create when it compiles the aspx markup into a class. What you're doing is setting the text property of an already-compiled control, so no additional compiling is going to occur.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top