c# create thead and tbody - c#

Can anyone tell me how to dynamically create thead tbody tags in my c# code?
private void MakeTable()
{
Table tb = new Table();
TableRow tr = new TableRow();
TableCell td = new TableCell();
td.Text="hello world";
tr.Cells.Add(td);
tb.Rows.Add(tr);
}
Thanks

Here a sample code that creates a THead, TBody and TFooter.
You can basically always use the TableRow object just reset the TableSection property.
Table table = new System.Web.UI.WebControls.Table();
TableRow tableRow;
TableCell tableCell;
tableRow = new TableRow();
tableRow.TableSection = TableRowSection.TableHeader;
tableCell = new TableCell();
tableCell.Text = "HEADER";
tableRow.Cells.Add(tableCell);
table.Rows.Add(tableRow);
tableRow = new TableRow();
tableRow.TableSection = TableRowSection.TableBody;
tableCell = new TableCell();
tableCell.Text = "BODY";
tableRow.Cells.Add(tableCell);
table.Rows.Add(tableRow);
tableRow = new TableRow();
tableRow.TableSection = TableRowSection.TableFooter;
tableCell = new TableCell();
tableCell.Text = "FOOTER";
tableRow.Cells.Add(tableCell);
table.Rows.Add(tableRow);
plhTest.Controls.Add(table);
Although I would suggest building the table in direct html and appending to page.

TableRow is basically tbody.
To make a thead section, use the TableHeaderRow class instead of a TableRow class.
(There is also, btw, TableFooterRow if you want to implement tfoot.

var row = new TableHeaderRow() { TableSection = TableRowSection.TableHeader };
table.Rows.Add(row);
should do the trick

Related

How do i add hyperlinks to my existing table cells

I have added a hyperlink to my existing Table cells but I get the filmname twice. Also i want to add other table cells as hyperlinks like Directors, Actors and Filmyear. how do i do that?
My Table Cells below:
tc = GetTableCell(film.FilmName);
tr.Cells.Add(tc);
tc = GetTableCell(film.Directors[0].PersonName);
tr.Cells.Add(tc);
tc = GetTableCell(film.Actors[0].PersonName);
tr.Cells.Add(tc);
tc = GetTableCell(film.FilmYear);
tr.Cells.Add(tc);
My HyperLink code below:
HyperLink link = new HyperLink();
link.NavigateUrl = "https://www.imdb.com/title/tt4154756/";
link.Text = film.FilmName;
tc.Controls.Add(link);
tr.Cells.Add(tc);
Try this.
private void CreateTable()
{
Table table = new Table();
TableRow tr = new TableRow();
//film name
tr.Cells.Add(GetTableCell("Film Name"));
//director
tr.Cells.Add(GetTableCell("Director Name"));
//actor
tr.Cells.Add(GetTableCell("Actor Name"));
//film year
tr.Cells.Add(GetTableCell("Film Year"));
//film link
HyperLink link = new HyperLink();
link.NavigateUrl = "https://www.imdb.com/title/tt4154756/";
link.Text = "Film Name";
tr.Cells.Add(GetTableCell(link));
table.Rows.Add(tr);
}
//returns new table cell with text content
private TableCell GetTableCell(string text)
{
TableCell tc = new TableCell();
tc.Text = text;
return tc;
}
//returns new table cell with hyperlink
private TableCell GetTableCell(HyperLink link)
{
TableCell tc = new TableCell();
tc.Controls.Add(link);
return tc;
}

Asp.net Web Forms Vertical Table

I have this code in my page in my ASP.New WebForms Project:
private void populateTable(MySqlDataReader mySqlDataReader)
{
foreach (DbDataRecord rowData in mySqlDataReader)
{
TableRow tr = new TableRow();
TableCell cell2 = new TableCell();
cell2.Text = rowData.GetInt32(rowData.GetOrdinal("truck_id")).ToString();
tr.Cells.Add(cell2);
TableCell cell3 = new TableCell();
cell3.Text = rowData.GetString(rowData.GetOrdinal("registration_no")).ToString();
tr.Cells.Add(cell3);
TableCell cell4 = new TableCell();
cell4.Text = rowData.GetString(rowData.GetOrdinal("make")).ToString();
tr.Cells.Add(cell4);
TableCell cell5 = new TableCell();
cell5.Text = rowData.GetString(rowData.GetOrdinal("model")).ToString();
tr.Cells.Add(cell5);
TableCell cell1 = new TableCell();
cell1.Text = rowData.GetString(rowData.GetOrdinal("engine_no")).ToString();
tr.Cells.Add(cell1);
TableCell cell6 = new TableCell();
cell6.Text = rowData.GetString(rowData.GetOrdinal("chassis_no")).ToString();
tr.Cells.Add(cell6);
TableCell cell7 = new TableCell();
cell7.Text = rowData.GetString(rowData.GetOrdinal("driver_name")).ToString();
tr.Cells.Add(cell7);
TableCell cell8 = new TableCell();
cell8.Text = rowData.GetString(rowData.GetOrdinal("driver_contact_no")).ToString();
tr.Cells.Add(cell8);
TableCell cell9 = new TableCell();
cell9.Text = rowData.GetString(rowData.GetOrdinal("status")).ToString();
tr.Cells.Add(cell9);
tbl_TruckData.Rows.Add(tr);
}
}
How can O intergrate it to Vertical Table?
Truck No 1
Registration No AA0-1234
The current out is an Horizontal Table like this:
Truck No Registration No Make
1 AA0-1234 MAN
Other question is it possible to embbed a <asp:Label> inside a <td> element?
For example:
<td> Make: </td>
<td> <asp:Label id = "TMake" runat="server"/></td>(this)
I've tried it but there's no out put in (this)
How can i intergrate it to Vertical Table.
You can start a new row each time:
TableRow tr = new TableRow();
TableCell cell2 = new TableCell();
cell2.Text = rowData.GetInt32(rowData.GetOrdinal("truck_id")).ToString();
tr.Cells.Add(cell2);
tbl_TruckData.Rows.Add(tr);
tr = new TableRow();
TableCell cell3 = new TableCell();
cell3.Text = rowData.GetString(rowData.GetOrdinal("registration_no")).ToString();
tr.Cells.Add(cell3);
tbl_TruckData.Rows.Add(tr);
or look into more sophisticated binding controls like GridView
Is it possible to embbed a <asp:Label> inside a <td> element?
Sure - you may not be setting the Text property - either in binding or in server-side code.
this.TMake.Text = make;

Execute method from dynamically created button [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to dynamically create a table with data and buttons acting as download links. On button click, I want a method to be called with arguments relevant to that individual button object.
foreach(var item in list_files) {
TableRow tRow = new TableRow();
file_table.Rows.Add(tRow);
TableCell namecell = new TableCell();
namecell.Text = item.name;
tRow.Cells.Add(namecell);
TableCell datecell = new TableCell();
datecell.Text = item.uTC;
tRow.Cells.Add(datecell);
TableCell sizecell = new TableCell();
sizecell.Text = item.size;
tRow.Cells.Add(sizecell);
Button downloadButton = new Button();
downloadButton.Text = "Download";
// code here to call a method named GetFile with two arguments from "item"
// e.g b.onclick ( GetFile(item.a, item.b)) ;
TableCell downloadlink = new TableCell();
downloadlink.Controls.Add(downloadButton);
tRow.Cells.Add(downloadlink);
}
foreach (var item in list_files)
{
TableRow tRow = new TableRow();
file_table.Rows.Add(tRow);
TableCell namecell = new TableCell(); namecell.Text = item.name; tRow.Cells.Add(namecell);
TableCell datecell = new TableCell(); datecell.Text = item.uTC; tRow.Cells.Add(datecell);
TableCell sizecell = new TableCell(); sizecell.Text = item.size; tRow.Cells.Add(sizecell);
Button downloadButton = new Button();
downloadButton.Text = "Download";
downloadButton.Click += (sender, evnt) =>
{
//do stuff here
GetFile(item.a, item.b)
};
// code here to call a method named GetFile with two arguments from "item"
// e.g b.onclick ( GetFile(item.a, item.b)) ;
TableCell downloadlink = new TableCell(); downloadlink.Controls.Add(downloadButton); tRow.Cells.Add(downloadlink);
}

Adding Image Caption

I have images displayed from a folder and with the help of the function below i am able to do just that.
private void LoadImages()
{
foreach (string strfile in Directory.GetFiles(Server.MapPath("~/Data")))
{
ImageButton imageButton = new ImageButton();
FileInfo fi = new FileInfo(strfile);
imageButton.ImageUrl = "~/Data/" + fi.Name;
imageButton.Height = Unit.Pixel(100);
imageButton.Style.Add("padding", "5px");
imageButton.Width = Unit.Pixel(100);
imageButton.Click += new ImageClickEventHandler(imageButton_Click);
Panel1.Controls.Add(imageButton);
}
}
Guys! i need to display the name of the image below the image. Help Please!
Wrap the ImageButton into a parent DIV element with the text label. A small table with two rows and 1 cell would work well also.
Example:
Table table = new Table();
TableRow row = new TableRow();
TableCell cell = new TableCell();
table.Controls.Add(row);
row.Controls.Add(cell);
cell.Controls.Add(imageButton);
row = new TableRow();
cell = new TableCell();
table.Controls.Add(row);
row.Controls.Add(cell);
cell.Text = "Image Text";

ASP.NET and C# fill a table with data from database

I've created a table with some C# and asp.net for be filled by data from a database, but when I add data to the table, the data is going to the same row. I want to put data in multiple rows.
Some code I have used is:
int tmp = 0;
TableRow tabelaLinhas = new TableRow();
while (tmp < dados.Rows.Count)
{
TableCell celula = new TableCell();
celula.Controls.Add(new LiteralControl(dados.Rows[tmp]["nome"].ToString()));
tabelaLinhas.Cells.Add(celula);
tabela.Rows.Add(tabelaLinhas);
CheckBox caixa = new CheckBox();
caixa.ID = dados.Rows[tmp]["nome"].ToString().Replace(" ", "").ToLower() + "CheckBox";
celula = new TableCell();
celula.Controls.Add((Control)caixa);
tabelaLinhas.Cells.Add(celula);
tabela.Rows.Add(tabelaLinhas);
tmp++;
}
Move
TableRow tabelaLinhas = new TableRow();
into the while loop; you are only creating one row object, even though you are adding multiple times. New instances need created in every while iteration.
HTH.
write
tabelaLinhas = new TableRow();
under
tabela.Rows.Add(tabelaLinhas);

Categories