How to display an ASP HTML code in a Label sending by Code-Behind (C #) - ASP.NET

3

I'm having a problem, I've done several tests where I'd like to send a HTML code directly to a label from a C # method, where it would dynamically display the code on the screen. But when I send it, it does not render.

    public partial class WebForm2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    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();                      
    }
}

But when the code that is sent to the label with the format it does not render on the screen. Only when it is sent this way:

public partial class WebForm2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    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();                      
    }
}

Neither does the LinkButton work.

How could you correct this error? Thank you all.

    
asked by anonymous 15.05.2015 / 02:21

1 answer

0

You're confusing some fundamental ASP.NET concepts here.

When you declare a <asp:LinkButton runat="server" /> tag, this tag is interpreted by the ASP.NET engine and an equivalent Html is generated for the page to be rendered. In this case, what you have in the final page, that is, what the browser displays is a <a> tag.

If you really need to put a server control at that point in the code, you would have to do it like this:

public void FillPage(int size)
{        
    LinkButton link = new LinkButton();
    link.ID = "LinkButton1";
    link.Click += LinkButton1_Click;
    link.CommandArgument = "MEU_ARGUMENTO"; // utilize essa propriedade se precisar passar algum parâmetro para o EventHandler

    HtmlTable table = new HtmlTable();
    table.Attributes.Add("class", "tableProduto");

    HtmlTableRow tr = new HtmlTableRow();
    HtmlTableCell td1 = new HtmlTableCell();
    td1.RowSpan = 2;
    td1.Width ="155px";

    HtmlImage img = new HtmlImage();
    img.Src = "images/categorias/bebida.png";

    td1.Controls.Add(img);

    HtmlTableCell td2 = new HtmlTableCell();
    td2.Width = "550px";
    td2.InnerText = "Nome";

    HtmlTableCell td3 = new HtmlTableCell();
    td3.RowSpan = 2;
    td3.InnerText = "Preço";

    tr.Cells.Add(td1);
    tr.Cells.Add(td2);
    tr.Cells.Add(td3);

    HtmlTableRow tr2 = new HtmlTableRow();
    HtmlTableCell td2Row2 = new HtmlTableCell();
    HtmlGenericControl div = new HtmlGenericControl("div");
    div.Attributes.Add("class", "divTexto");

    HtmlGenericControl p = new HtmlGenericControl("p");
    p.InnerText = "Descrição";

    div.Controls.Add(p);
    td2Row2.Controls.Add(div);
    tr2.Cells.Add(td2Row2);

    table.Rows.Add(tr);
    table.Rows.Add(tr2);

    link.Controls.Add(table);

    lblTexto.Controls.Add(link);
}

protected void LinkButton1_Click(object sender, EventArgs e)
{
      // recupera argumento, caso tenha sido passado
      string argument = (sender as LinkButton).CommandArgument;
}

Keep in mind that dynamically created controls should be re-created at each postback, otherwise they will be lost.

    
15.05.2015 / 02:57