How to add a dynamically link in a dynamically added table - c#

I'm a newbie and struggle to add a link, showed like an img in a dynamically added table.
string search = Search.Text;
IMyData members = new MyData();
DataTable dt = new DataTable();
dt = members.Search(search);
Table t = new Table();
t.ID = "tblTable";
TableRow row = null;
for (int i = 0; i < dt.Rows.Count; i++)
{
HyperLink link = new HyperLink();
row = new TableRow();
for (int j = 0; j < dt.Columns.Count; j++)
{
TableCell cell = new TableCell();
if (j == dt.Columns.Count - 1) //This last field may hava a number
{
if (Convert.ToInt32(dt.Rows[i][j].ToString()) > 0)
{
link.ID = "link" + i + "_" + j;
link.NavigateUrl = "members.aspx?showLease=" + dt.Rows[i][j].ToString();
link.ImageUrl = "img/document.png";
Page.Controls.Add(link); // How to put this in a cell, not on page
}
else
{
cell.Text = dt.Rows[i][j].ToString();
}
}
row.Cells.Add(cell);
}
t.Rows.Add(row);
}
pnlTable.Controls.Add(t);
How can I put the Hyperlink to the cell, and not to the Page?
Thanks

You can add control in TableCell the way you are doing in Page. Change your code like this
Page.Controls.Add(link);//Will add control in page
cell.Controls.Add(link);//Will add control in table cell

See below, I changed Page.Controls.Add(link) to cell.Controls.Add(link) and moved your Hyperlink declaration into the cell loop. Otherwise if will only be added in the last cell.
But if I see your code it seems that only the last cell will have link or text because of the j == dt.Columns.Count - 1
for (int i = 0; i < dt.Rows.Count; i++) {
row = new TableRow();
for (int j = 0; j < dt.Columns.Count; j++)
{
HyperLink link = new HyperLink();
TableCell cell = new TableCell();
if (j == dt.Columns.Count - 1) //This last field may hava a number
{
if (Convert.ToInt32(dt.Rows[i][j].ToString()) > 0)
{
link.ID = "link" + i + "_" + j;
link.NavigateUrl = "members.aspx?showLease=" + dt.Rows[i][j].ToString();
link.ImageUrl = "img/document.png";
cell.Controls.Add(link); // How to put this in a cell, not on page
}
else
{
cell.Text = dt.Rows[i][j].ToString();
}
}
row.Cells.Add(cell);
}
t.Rows.Add(row);
}

Related

How can i create col span or row span on dynamic table

public partial class WebForm1 : System.Web.UI.Page
{
}
protected void Page_Load(object sender, EventArgs e)
{
}
public void CreateRuntime_Table()
{
int tblRows = int.Parse(txtrow.Text);
int tblCols = int.Parse(txtcol.Text);
Table tbl = new Table();
tbl.BorderWidth = 3;
tbl.BorderStyle = BorderStyle.Solid;
tbl.ID = "myTable";
for (int i = 1; i <= tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 1; j <= tblCols; j++)
{
TableCell tc = new TableCell();
TextBox txtbox = new TextBox();
txtbox.Text = "Test Row:" + i + "Test Col:" + " " + j;
//Add the control to the table cell
tc.Controls.Add(txtbox);
tr.Controls.Add(tc);
}
tbl.Rows.Add(tr);
}
form1.Controls.Add(tbl);
}
protected void Unnamed_Click(object sender, EventArgs e)
{
CreateRuntime_Table();
}
I have created a dynamic table using c#. How do I set a colspan on the dynamic table using a textbox?
For example if I place a value 3 in the text box and click on Apply Span button then the colspan of a dynamic table should change accordingly.
Check the Image below.
I am new to c#, pls help
Thanks.
UI Image
HTML col-span and row-span attributes are represented by ColumnSpan and RowSpan properties of TableCell class.
In your code, you should add assigning of those properties to TableCell tc.
public void CreateRuntime_Table()
{
int tblRows = int.Parse(txtrow.Text);
int tblCols = int.Parse(txtcol.Text);
//I would recommend using int.TryParse with some defaults
int colSpan = 0;
int rowSpan = 0;
int.TryParse(tbColspanName.Text, out colSpan);
int.TryParse(tbRowspanName.Text, out rowSpan);
Table tbl = new Table();
tbl.BorderWidth = 3;
tbl.BorderStyle = BorderStyle.Solid;
tbl.ID = "myTable";
for (int i = 1; i <= tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 1; j <= tblCols; j++)
{
TableCell tc = new TableCell()
{
//assign entered col / row span
ColumnSpan = colSpan,
RowSpan = rowSpan
};
TextBox txtbox = new TextBox();
txtbox.Text = "Test Row:" + i + "Test Col:" + " " + j;
//Add the control to the table cell
tc.Controls.Add(txtbox);
tr.Controls.Add(tc);
}
tbl.Rows.Add(tr);
}
form1.Controls.Add(tbl);
}

Using CSS how to have row of tables followed by row of tables

I have an Array of Tables. For example 6 x 6. and a PlaceHolder.
I need to place the tables 6 next to each other in the PlaceHolder and then a new line of 6 next to each other, etc. What css properties do I add for each grid to achieve this.
I have
LiteralControl ltr = new LiteralControl();
ltr.Text = "<style type=\"text/css\" rel=\"stylesheet\">" + #".fl { float: left}</style>";
this.Page.Header.Controls.Add(ltr);
Table[,] tableArray = new Table[6,6];
for (int j = 0; j < tableArray.GetLength(0); j++)
{
bool first = true;
for (int i = 0; i < tableArray.GetLength(1); i++)
{
if (first)
{
tableArray[j, i].CssClass = "mGrid";
first = false;
}
else
{
tableArray[j, i].CssClass = "fl mGrid";
}
tableArray[j, i].Width = Unit.Percentage(100 / 6);
PlaceHolderTables.Controls.Add(tableArray[j, i]);
}
}
But I do not know how to start a new row and then have 5 next to it etc. I am inexperienced with CSS. The tables has been initialised else where. mGrid is defined elsewhere as well.
You can place those 36 tables inside a table with 6x6.
protected void Page_Load(object sender, EventArgs e)
{
LiteralControl ltr = new LiteralControl();
ltr.Text = "<style type=\"text/css\" rel=\"stylesheet\">" + #".fl { float: left}</style>";
Page.Header.Controls.Add(ltr);
Table main = new Table();
for (int i = 0; i < 6; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < 6; j++)
{
Table table = CreateTable($"{i}x{j}");
TableCell cell = new TableCell();
cell.Controls.Add(table);
row.Controls.Add(cell);
}
main.Controls.Add(row);
}
PlaceHolderTables.Controls.Add(main);
}
private Table CreateTable(string text)
{
TableCell cell = new TableCell();
cell.Controls.Add(new Literal {Text = text });
TableRow row = new TableRow();
row.Cells.Add(cell);
Table table = new Table();
table.Rows.Add(row);
return table;
}

to add dropdown in a table dynamically

I have a table which i cant fill up with data dynamically ... it only tends to show the last table cell the rest rows are empty.The dropdowndownlist seems to popuate in the right way though.
DataTable dtt1 = (DataTable)Session["dbasecolumns"];
int l = dtt1.Columns.Count;
string[] sqlcolumn = new string[l];
***for (int j = 0; j < dtt1.Columns.Count; j++)
{
sqlcolumn[j] = dtt1.Columns[j].ColumnName;
}
Session["excel"] = sqlcolumn;***
DropDownList drd = new DropDownList();
foreach (String colname in sqlcolumn)
{
drd.Items.Add(colname);
}
Table mytable = new Table();
mytable.Visible = true;
for (int rowctr = 0; rowctr < sqlcolumn.Length; rowctr++)
{
TableRow myrow = new TableRow();
mytable.Rows.Add(myrow);
for (int cellctr = 0; cellctr < 1; cellctr++)
{
TableCell mycell = new TableCell();
mycell.Controls.Add(drd);
myrow.Cells.Add(mycell);
}
***mytable.Rows.Add(myrow);***
}
Panel1.Controls.Add(mytable);
Thanks in Advance
DataTable dtt1 = (DataTable)Session["dbasecolumns"];
int l = dtt1.Columns.Count;
string[] sqlcolumn = new string[l];
DropDownList drd = new DropDownList();
foreach (String colname in sqlcolumn)
{
drd.Items.Add(colname);
}
You are iterating over sqlcolumn but your sqlcolumn is empty. It does not have any values to fill up your DropDownList. Here you have defined your string array of a particular length but it does not contains any value. (Hope I am on right direction as I can see only this much of your code).
You can directly fill your DropDownList from a datatable.
DataTable dtt1 = (DataTable)Session["dbasecolumns"];
DropDownList drd = new DropDownList();
for (int i = 0; dtt1 .Rows.Count > i; i++)
{
dhgdh.Items.Add(dt.Rows[i]["dbasecolumns"].ToString());
}
This will work
for (int rowctr = 0; rowctr <= sqlcolumn.Length; rowctr++)
{
TableRow myrow = new TableRow();
mytable.Rows.Add(myrow);
// for (int cellctr = 0; cellctr <= 1; cellctr++)
//{
DropDownList drd = new DropDownList();
foreach (String colname in sqlcolumn)
{
drd.Items.Add(colname);
}
TableCell mycell = new TableCell();
mycell.Controls.Add(drd);
myrow.Cells.Add(mycell);
//}
mytable.Rows.Add(myrow);
}
i added a table with two columns.Each column has a dropdownlist with the listitems from a SQL database and an Excel file.
for (int rowctr = 0; rowctr <= sqlcolumn.Length; rowctr++)
{
mycell = new TableCell();
TableCell mycell1 = new TableCell();
for (int cellctr = 0; cellctr < 1; cellctr++)
{
DropDownList drd = new DropDownList();
DropDownList drd1 = new DropDownList();
foreach (String colname in sqlcolumn)
{
drd.Items.Add(colname);
}
foreach (string colnames in excel)
{
drd1.Items.Add(colnames);
}
TableRow myrow = new TableRow();
mycell.Controls.Add(drd);
mycell1.Controls.Add(drd1);
myrow.Cells.Add(mycell);
myrow.Cells.Add(mycell1);
mytable.Rows.Add(myrow);
}
Panel1.Controls.Add(mytable);

Dynamic Table cell to be filled when triggering a method

I'm working with ASP.NET an C# I have a dynamic table and I want a cell to have an initial value then I want to use this value as a parameter in a method and fill the same cell with another value....
here is snippet of my code
for (int index = 0; index < size; index++) {
List<Hotel> h = listHotelList[index];
Hotel myHotel = new Hotel();
mytable = new Table();
mytable.ID = "HotelTable"+index;
Page.Form.Controls.Add(mytable);
mytable.CellSpacing = 20;
mytable.CellPadding = 10;
for (int g = 0; g < h.Count; g++)
{
myHotel = h[g];
TableRow row = new TableRow();
for (int i = 0; i < 2; i++)
{
TableCell cell = new TableCell();
if (i == 0)
{
// I want to leave this with only the value of the hotelId
// and I want my method to fill this cell with an image by retrieving
// the value of the hotelId
}
if (i == 1)
{
Label tb = new Label();
tb.ID = "label1_" + g + "Col" + i;
tb.Text = "<h4>" + myHotel.hotelName + "</h4><br />";
cell.Controls.Add(tb);
}
row.Cells.Add(cell);
}
mytable.Rows.Add(row);
}
}
for my case I'm taking a hotel Id from the cell when the user click a button then it trigger a method that would take the value in the cell "hotel Id" and retrieve the image for this particular hotel and fill the cell with the image
Is the problem that you don't see the tabel?
If so,
add the table to your page
Page.Controls.Add(mytable);
If the positioning is not good, then put a placeholder on your page, and add the control there.
Placeholder.Controls.Add(mytable);

Creating tables and displaying textboxes in codebehind

I have a Dataset from the database which i want to display. My aim is to display a month's data on an aspx page using VS 2005 with 5 days per each row. I have written the code like this but i am confused with the i and j. This code displays nothing.
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
Table table = new Table();
table.ID = "Table1";
TableRow row = new TableRow();
TableCell cell = new TableCell();
TextBox tb1 = new TextBox();
TextBox tb2 = new TextBox();
// I am not sure what i and j should be here to display 5 per each row..
for (int i = 0; i < 5; i++)
{
if (int j == 0; j < ds.Tables[0].Rows.Count; j ++)
{
tb1.ID = "txtDateRow" + x + "Col" + j;
tb1.Text = ds.Tables[0].Rows[x]["Date"].ToString();
tb2.ID = "txtDetails" + x + "Col" + j;
tb2.Text = ds.Tables[0].Rows[x]["AmountSold"].ToString();
cell.Controls.Add(tb1);
cell.Controls.Add(tb2);
table.Rows.Add(row);
}
}
Panel1.Controls.Add(table);
}
If someone could help me solve this, i really appreciate it. Thanks a lot.
row.Controls.Add(cell) is missing. Bcoz of this textbox controls are not added to the table and you are not able to see anything. Add this line and it will help you see. Later you can think about i & j.
You have a loop with a counter, namely, i but you test for j==0 what is j?
Is j initalized somewhere? Is j even declared? If not, are you sure you don't want to test for i == 0 ?
That is the reason you are not getting any results in the for loop.
The code:
tb1.ID = "txtDateRow" + x + "Col" + j;
tb1.Text = ds.Tables[0].Rows[x]["Date"].ToString();
tb2.ID = "txtDetails" + x + "Col" + j;
tb2.Text = ds.Tables[0].Rows[x]["AmountSold"].ToString();
cell.Controls.Add(tb1);
cell.Controls.Add(tb2);
table.Rows.Add(row);
Is inside a condition:
if (j == 0)
The question is does this condition ever happen? If so you need to post more code.
Debug your code and set breakpoints and watch how it runs. Per your edit you posted:
if (int j == 0; j < ds.Tables[0].Rows.Count; j ++)
This is not even valid, I think you need to pick up a book on C# syntax before you tackle this problem.
I'm not sure what 'x' is supposed to be in your code. I think the general structure should be something like this but the code below will add the same thing for each column:
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
Table table = new Table();
table.ID = "Table1";
// j is the row index
for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
{
TableRow row = new TableRow();
// i is the column index
for (int i = 0; i < 5; i++)
{
TableCell cell = new TableCell();
TextBox tb1 = new TextBox();
TextBox tb2 = new TextBox();
tb1.ID = "txtDateRow" + j + "Col" + i;
tb1.Text = ds.Tables[0].Rows[j]["Date"].ToString();
tb2.ID = "txtDetails" + j + "Col" + i;
tb2.Text = ds.Tables[0].Rows[j]["AmountSold"].ToString();
cell.Controls.Add(tb1);
cell.Controls.Add(tb2);
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
Panel1.Controls.Add(table);
}

Categories

Resources