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);
}
Related
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;
}
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 ...
}
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);
}
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);
I was trying to create a table dynamically and put textboxes in it. Here in the following code, i was trying to create a textbox with a distinct name for each k. But only the last value of k is getting displayed in the textbox. I was wondering how i could give a name to Textbox so all are shown.
for (int k = 0; k < tblCols; k++)
{
TableCell tc = new TableCell();
TextBox txtCompanyName = new TextBox();
txtCompanyName.Text = dsmissing.Tables[0].Rows[tblCols- 1]["NewCompanyName"].ToString();
tc.Controls.Add(txtCompanyName);
}
Replace this line
dsmissing.Tables[0].Rows[tblCols- 1]["NewCompanyName"].ToString();
With
dsmissing.Tables[0].Rows[k]["NewCompanyName"].ToString();
I assume you are talking about ID like below.
for (int k = 0; k < tblCols; k++)
{
TableCell tc = new TableCell();
TextBox txtCompanyName = new TextBox();
txtCompanyName.Text = dsmissing.Tables[0].Rows[k]["NewCompanyName"].ToString();
txtCompanyName.ID = Guid.NewGuid().ToString("N");
tc.Controls.Add(txtCompanyName);
}
I think you may have meant to do this:
for (int k = 0; k < tblCols; k++)
{
TableCell tc = new TableCell();
TextBox txtCompanyName = new TextBox();
//txtCompanyName.Text = dsmissing.Tables[0].Rows[tblCols-1 ["NewCompanyName"].ToString();
txtCompanyName.Text = dsmissing.Tables[0].Rows[k]["NewCompanyName"].ToString();
tc.Controls.Add(txtCompanyName);
}
Surely this will work and it is easy to understand
protected void Page_Load(object sender, EventArgs e)
{
button1();
}
protected void Page_Init(object sender, EventArgs e)
{
try
{
Label lbl = new Label();
lbl.ID = "lbl_label";
lbl.Text = "Enter the values";
form1.Controls.Add(lbl);
TextBox tb = new TextBox();
tb.ID = "tbx_textbox";
form1.Controls.Add(tb);
Button bt = new Button();
bt.ID = "bt_button";
bt.Text = "click";
form1.Controls.Add(bt);
}
catch (Exception ex) { }
}
public void button1()
{
Table table = new Table();
TableRow row = null;
TableCell cell = null;
TextBox tbx1 = this.Page.FindControl("tbx_textbox") as TextBox;
try
{
int a = int.Parse(tbx1.Text);
for (int i = 0; i < a; i++)
{
row = new TableRow();
cell = new TableCell();
TextBox tx = new TextBox();
tx.ID = "box" + i.ToString();
cell.Controls.Add(tx);
row.Cells.Add(cell);
table.Rows.Add(row);
form1.Controls.Add(table);
}
}
catch (Exception ex) { }
finally
{
table = null;
}
}