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

Asked

Viewed 1,017 times

3

I’m having a problem, I did several tests where I wanted to send directly from a C# method an HTML code to a label, where it would display dynamically the code on the screen. But when I tell her to, she won’t 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();                      
    }
}

Even Linkbutton doesn’t work.

How could I correct that mistake? Thank you all.

1 answer

0


You’re confusing some core ASP.NET concepts here.

When you declare a tag <asp:LinkButton runat="server" />, 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 on the end page, that is, what the browser displays is a tag <a>.

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

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 recreated every postback, otherwise they will be lost.

  • Thank you so much!!!! It was of immense help Marcus!

  • Read more about creating dynamic controls in http://www.codeproject.com/Articles/8114/Dynamically-Created-Controls-in-ASP-NET and https://msdn.microsoft.com/en-us/library/kyt0fzt1(v=vs.100). aspx

  • Marcus, I just have one more question, I wanted to use these tables to fill with information in the database and so make it clickable to send me to another page. I would use a Foreach to create several of these tables, only I’m not able to create an On_click Event for each linkButton, as I could do this?

  • Do you really need each link to do a different action? What action would that be?

  • I need the following : Each table will be filled with product information, then each table would have a click event where it would call a method and send as argument the "ID" of this product, where it would call another page and fill the spaces with product information. I soon thought it would be feasible to make a method for each link button, where each one would send a certain parameter of their product

  • You can use the property CommandArgument of LinkButton to store the ID (link.CommandArgument = idProduto.ToString();). Then, inside the Eventhandler, to recover the id, would do something like this: string idProduto = (sender as LinkButton).CommandArgument;. I edited the answer to include this solution.

  • 1

    It worked perfectly! Thank you very much Marcus, even if it wasn’t for you I don’t know what would become of me, I am developing a project with little knowledge about Asp, people like you with good will that motivate me to continue and study! Thank you, and all the best!

  • I’m happy to help!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.