Dynamic Table with label and button as Columns - c#

I want to create a dynamic table in C# with Adding and deleting of row dynamically in which I need two columns, Column 1 as label and column 2 as button and I want that table to be add rows from some other methods..
and Clicking of the button of column 2 , I want particular row to be delete only..
for example:
COL1 COL2
label button(to delete row)
(add)
I am trying is:
In aspx
<asp:Table border="1" id="Table1" runat="server">
</asp:Table>
in C#
for (int i = 0; i < 3; i++)
{
TableRow row = new TableRow();
for (int j = 0; j <2; j++)
{
TableCell cell = new TableCell();
// Add the control to the TableCell
LinkButton tb1 = new LinkButton();
if (IsOdd(j))
{
Label tb = new Label();
tb.Width = 100;
// Set a unique ID for each zTextBox added
tb.ID = "TextBoxRow_" + i + "Col_" + j;
tb.Text = "asdad";
cell.Controls.Add(tb);
}
else
{
tb1.Width = 100;
// Set a unique ID for each zTextBox added
tb1.ID = "TextBoxRow_" + i + "Col_" + j;
tb1.Text = "asdad";
cell.Controls.Add(tb1);
}
// Add the TableCell to the TableRow
row.Cells.Add(cell);
}
Table1.Rows.Add(row);
}

Related

way to put linkbutton into tablecell, asp.net

Here is the part of a code which builds DB from SQL query, but I need to put extra cell for LinkButton.
for (int j = 0; j < numrows; j++)
{
TableRow row = new TableRow();
for (int i = 0; i < numcols; i++)
{
TableCell cell = new TableCell();
string textContent;
textContent = subjects.Rows[j][i].ToString();
cell.Text = textContent;
row.Cells.Add(cell);
}
TableCell cell1 = new TableCell();
LinkButton button = new LinkButton();
button.Text = "look";
button.Click += new EventHandler(submit_Click2);
cell1. = button;
row.Cells.Add(cell1);
table.Rows.Add(row);
}
The question is that I don't know what do to with this part of a code where I put button into cell, I couldn't find a way to do it. Is it possible to do it without GridView, because I can't use it?
TableCell cell1 = new TableCell();
LinkButton button = new LinkButton();
button.Text = "look";
button.Click += new EventHandler(submit_Click2);
cell1. = button;
Here you go, a simple example. But remember to not add the table to the page within IsPostBack
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//do not add dynamic controls like the linkbutton in the table in "IsPostBack"
}
//to generate some random data
var random = new Random();
//number of rows and cells
var rowCount = random.Next(5, 25);
var cellCount = random.Next(3, 10);
//create a new table
var table = new Table()
{
CssClass = "table table-striped"
};
//add the rows
for (int i = 0; i < rowCount; i++)
{
var row = new TableRow();
//add the cells
for (int j = 0; j < cellCount; j++)
{
//create a new cell
var cell = new TableCell();
//if the cell is not the last add content, otherwise add the linkbutton
if (j < cellCount - 1)
{
cell.Text = "CellValue " + random.Next(1000, 100000).ToString();
}
else
{
//create a new linkbutton
var lb = new LinkButton()
{
Text = "LinkButton"
};
//add the click event handler
lb.Click += new EventHandler(LinkButton_Click);
//add the linkbutton to the cell
cell.Controls.Add(lb);
}
//add the cell to the row
row.Cells.Add(cell);
}
//add the row to the table
table.Rows.Add(row);
}
//add the table to the page
PlaceHolder1.Controls.Add(table);
}

How to change the border color of a single cell using dynamic table

I am creating a dynamic table and I want to change the border color of a specific cell e.g (0,1).I cannot figure out on how will access the specific cell? Any idea?
protected void cmdCreate_Click(object sender, EventArgs e)
{
//clear the table
tbl.Controls.Clear();
int rows = Int32.Parse(txtRows.Text);
int cols = Int32.Parse(txtCols.Text);
for (int row = 0; row < rows; row++)
{
TableRow rowNew = new TableRow();
tbl.Controls.Add(rowNew);
tbl.Rows.AddAt(0, TableRow row);
for (int col = 0; col < cols; col++)
{
//create a new tablecell object
TableCell cellNew = new TableCell();
cellNew.Text = "Example Cell (" + row.ToString() + "," + col.ToString() + ")";
cellNew.BorderColor = System.Drawing.Color.Red;
// cellNew.Text += col.ToString() + ")";
if (chkBorder.Checked)
{
cellNew.BorderStyle = BorderStyle.Inset;
cellNew.BorderWidth = Unit.Pixel(1);
}
rowNew.Controls.Add(cellNew);
}
}
}
You can check which at which cell you are by checking row and col. And you need to specify the BorderStyle also, not just the color.
if (row == 0 && col == 1)
{
cellNew.BorderColor = System.Drawing.Color.Red;
cellNew.BorderStyle = BorderStyle.Solid;
}

How do I add table header dynamically in c#?

I have successfully created generating new records into table rows but want to add table header, so how to do that?
private void GenerateTable(int rowsCount)
{
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
const int colsCount = 3;
for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
tb.ID = "TextBoxRow_" + i + "Col_" + j;
cell.Controls.Add(tb);
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
SetPreviousData(rowsCount, colsCount);
rowsCount++;
ViewState["RowsCount"] = rowsCount;
}
You do it like that:
TableHeaderRow header = new TableHeaderRow(); // Creating a header row
table.Rows.Add(header); // Add the header row to table tbl
For adding cells in the header row here is an example:
TableHeaderCell headerTableCell1 = new TableHeaderCell();
header.Cells.Add(headerTableCell1);
And this is where is goes in your code:
private void GenerateTable(int rowsCount)
{
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
const int colsCount = 3;
TableHeaderRow header = new TableHeaderRow();
table.Rows.Add(header);
//These two lines in iteir own loop
TableHeaderCell headerTableCell1 = new TableHeaderCell();
header.Cells.Add(headerTableCell1);
for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
tb.ID = "TextBoxRow_" + i + "Col_" + j;
cell.Controls.Add(tb);
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
SetPreviousData(rowsCount, colsCount);
rowsCount++;
ViewState["RowsCount"] = rowsCount;
}
Building on the suggested solution by #israel-altar, I struggled with adding the header into a <thead> tag and not into a <tbody>.
To achieve this, you have to use the TableSelection property of your TableHeaderRow:
private void GenerateTable(List<string> fieldsList)
{
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
TableHeaderRow header = new TableHeaderRow();
foreach (string field in fieldsList)
{
TableHeaderCell headerTableCell1 = new TableHeaderCell();
headerTableCell1.Controls.Add(new Literal() {
Text = field
});
header.Cells.Add(headerTableCell1);
}
// Set TableSection to TableHeader so that the TableHeaderRow is added within <thead> and not within <tbody>
header.TableSection = TableRowSection.TableHeader;
table.Rows.Add(header);
// loop rows ...
}

how to get cell selected from dynamically generated html table with links

This is how im creating an table an adding link button for every cell. Im stuck in getting the selected cell value from the table
int noofquestions = 60;
int k = 1;
for (int i = 1; i <= noofquestions / 5; i++)
{
HtmlTableRow tRow = new HtmlTableRow();
//TableRow tRow = new TableRow();
tblNavigation.Rows.Add(tRow);
for (int j = 1; j <= 5; j++)
{
HtmlTableCell tCell = new HtmlTableCell();
//TableCell tCell = new TableCell();
//tCell.Text = "Row " + i + ", Cell " + j;
tRow.Cells.Add(tCell);
//tCell.InnerText = k.ToString();
LinkButton link = new LinkButton();
//HyperLink link = new HyperLink();
link.Text = k.ToString();
tCell.Controls.Add(link);
k = k + 1;
}

How to create nested html table in c# using recursion logic?

I just tried over it but i got the HTML tables in the manner of unique,i just need the tables to created one within another.
sample output: http://i.stack.imgur.com/drdXx.jpg][1]sample
In Design Page
<table id="tableContent" border="1" runat="server"></table>
protected void Page_Load(object sender, EventArgs e)
{
var s = 0;
Addtable(s);
}
public void Addtable(int j)
{
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
cell.InnerText = "col 1";
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = "col2";
row.Cells.Add(cell);
tableContent.Rows.Add(row);
tableContent.Border =1;
if( j < 5)
{
j++;
Addtable(j);
}
}
You need to change your recursion function as below
public void Addtable(HtmlTable baseTable, int j)
{
HtmlTable innerTable = new HtmlTable();
// change to stylesheet instead. Just added as an example to get the output
baseTable.Style.Add("margin-left", "25px");
baseTable.Style.Add("margin-right", "25px");
baseTable.Style.Add("margin-bottom", "25px");
baseTable.Style.Add("text-align", "center");
baseTable.Border = 1;
//Create a container cell for inner table
HtmlTableRow container = new HtmlTableRow();
HtmlTableCell containerCell = new HtmlTableCell();
Literal l = new Literal();
l.Text = "Table " + j;
containerCell.Controls.Add(l);
containerCell.Controls.Add(innerTable);
containerCell.ColSpan = 2;
container.Cells.Add(containerCell);
baseTable.Rows.Add(container);
if (j < 5)
{
j++;
Addtable(innerTable, j);
}
}
Then call the function
var s = 0;
Addtable(tableContent,s);

Categories