简体   繁体   中英

How to show a HTML ASP Code on a label sending from Code-Behind

I'm having a problem, which I want to send ASP HTML code to a Label from a method on Code-Behind, to make the label shows dynamically the HTML code on screen. But when I send it only works when the code Isn't <asp:> just HTML.

public void FillPage(int size)
{
    StringBuilder sb = new StringBuilder();

    sb.Append(string.Format(@"<asp:LinkButton ID='LinkButton1' runat='server' OnClick='LinkButton1_Click'><asp:Table ID='tableProd' class='tableProduto' runat='server'>
   <asp:TableRow>
       <asp:TableCell RowSpan='2' Width='155px'><img src='images/categorias/bebida.png' /></asp:TableCell>             
       <asp:TableCell Width='550px'>Nome</asp:TableCell>
       <asp:TableCell RowSpan='2'>Preço</asp:TableCell>
   </asp:TableRow>

    <asp:TableRow>       
       <asp:TableCell Width='550px'><div class='divTexto'><p>Descrição</p></div></asp:TableCell>             
    </asp:TableRow>          
</asp:Table>
</asp:LinkButton> "));

    lblTexto.Text = sb.ToString();                      
}
}

It doesn't works. But when I do the following code it works:

public void FillPage(int size)
{
    StringBuilder sb = new StringBuilder();

    sb.Append(string.Format(@"<asp:LinkButton ID='LinkButton1' runat='server' OnClick='LinkButton1_Click'><table ID='tableProd' class='tableProduto' runat='server'>
   <tr>
       <td RowSpan='2' Width='155px'><img src='images/categorias/bebida.png' /></td>             
       <td Width='550px'>Nome</td>
       <td RowSpan='2'>Preço</td>
   </tr>

    <tr>       
       <asp:TableCell Width='550px'><div class='divTexto'><p>Descrição</p></div></td>             
    </tr>          
</table>
</asp:LinkButton> "));

    lblTexto.Text = sb.ToString();                      
}
}

But I need the controls of LinkButton, which isn't working.

You can't do that.

"ASP Codes" can only be interpreted on the server. By putting them in the label, you're requiring the browser to understand them - it won't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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