Question:
I'm having a problem, I've done several tests where I'd want to send HTML code directly from a C# method to a label, where it would dynamically display the code on the screen. But when I send it, it doesn'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 doesn't render to the screen. Only when it's 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();
}
}
Not even the LinkButton works.
How could I fix this error? Thank you all.
Answer:
You're confusing some fundamental ASP.NET concepts here.
When you declare a <asp:LinkButton runat="server" />
tag, that tag is interpreted by the ASP.NET engine and an Html equivalent is generated for the page to be rendered. In this case, what you have on the final page, that is, what the browser displays is an <a>
tag.
If you really need to put a server control at this 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 must be recreated on each postback, otherwise they will be lost.