This has been puzzling me for a while. I have a label
in asp (C#) where I created a table to store the values received from an sql database. It looks like this (this is the loop that gathers the sql values and puts them in a table structure):
while (reader.Read())
{
Label1.Text += "<tr><td>"
+ reader[0] + "</td><td>"
+ reader[1] + "</td><td>"
+ reader[2] + "</td><td>"
+ reader[3] + "</td><td>"
+ reader[4] + "</td><td>"
+ reader[5] + "</td><td>";
Button btn = new Button();
btn.ID = "del" + reader[0];
btn.Text = "Borrar";
this.Label1.Controls.Add(btn);
Label1.Text += "</td></tr>";
}
I omit the common parts of the table (they are outside the loop).
The puzzling question is: The table appears as content of Label1
, but attaching the buttons to that label doesn't work. If I attach them to
this.form1.Controls.Add(btn)
then it attaches them to the main form, outside the table.
I am thinking whether it's mandatory to create the table with own asp instructions and proper IDs in order to do this, or you can maybe suggest a way to achieve it with my current way of thinking.
Added more explanation : The purpose of the buttons is to delete the row (Which is being acquired from a Compact SQL database). I know you can achieve the same with a GridView, but I had a different problem with that method (see here ) so I switched to SqlCeDataAdapter
and SqlCeDataCommand
, which are working very nicely!
My only problem was about how to insert the buttons in that context, but I am now in the middle of converting the table to an actual asp table with runat=server
(rather than classic html), done programmatically, hoping that it can solve the problem. In the meantime, suggestions about fixing my initial idea or using a different way would be very welcome!
Solved! Here is an example of how to create a button dynamically and then place it correctly into a cell. The example assumes that you have also created the rest of the table using the same system:
Button btnd = new Button();
btnd.ID = "del" + reader[0];
btnd.Text = "Borrar";
TableCell cedit = new TableCell();
cedit.ID = "edit" + reader[0];
cedit.Controls.Add(btnd);
//I previously created the row 'r'
r.Cells.Add(cedit);
//I am inserting in Table2
Table2.Rows.Add(r);
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.