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.
Thank you so much!!!! It was of immense help Marcus!
– Alexandre
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 Vinicius
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?
– Alexandre
Do you really need each link to do a different action? What action would that be?
– Marcus Vinicius
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
– Alexandre
You can use the property
CommandArgument
ofLinkButton
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.– Marcus Vinicius
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!
– Alexandre
I’m happy to help!
– Marcus Vinicius