Dynamic Table cell to be filled when triggering a method - c#

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);

Related

Generate a grid of (ListView)'s in Visual Studio c#

I'm trying to create a panel(Static) in C#, were I generate multiple ListViews/GridViews in a Grid.
I know how to Fill a single existing ListView.
I've done this multiple times on ListViews I dragged onto my application using the toolbox.
I have a sqldatabase connection and I want to use data from that database to determine how many ListViews/GridViews are going to be generated.
I found a picture of what I am imagining in my head(Without the roomstatus)
If there are more ListViews/GridViews generated I want to be able to scroll down INSIDE the panel i created.
Okay, i've managed to pull it of!
private void GenerateTable()
{
SomerenLogic.Room_Service roomService = new SomerenLogic.Room_Service();
List<Room> roomList = roomService.GetRooms();
int counter = 0;
foreach (SomerenModel.Room s in roomList)
{
counter = counter + 1;
}
double wortelvan = Math.Sqrt(counter);
double column = Math.Floor(wortelvan);
double row = Math.Ceiling(wortelvan);
int columnCount = (int)(column);
int rowCount = (int)(row);
SomerenLogic.Indeling_Service indelingService = new SomerenLogic.Indeling_Service();
//Clear out the existing controls, we are generating a new table layout
tableLayoutPanel1.Controls.Clear();
tableLayoutPanel1.RowStyles.Clear();
//Clear out the existing row and column styles
tableLayoutPanel1.ColumnStyles.Clear();
tableLayoutPanel1.RowStyles.Clear();
//Now we will generate the table, setting up the row and column counts first
tableLayoutPanel1.ColumnCount = columnCount;
tableLayoutPanel1.RowCount = rowCount;
int counter2 = 1;
for (int x = 0; x < columnCount; x++)
{
//First add a column
tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent));
for (int y = 0; y < rowCount; y++)
{
//Next, add a row. Only do this when once, when creating the first column
if (x == 0)
{
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent));
}
////Create the control, in this case we will add a button
//Button cmd = new Button();
//cmd.Text = string.Format("({0}, {1})", x, y);
////Finally, add the control to the correct location in the table
ListView listView1 = new ListView();
listView1.Columns.Add("");
listView1.View = View.Details;
listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
listView1.Scrollable = false;
listView1.GridLines = true;
listView1.AutoArrange = true;
List<Indeling> indelingList = indelingService.GetIndeling(counter2);
foreach (SomerenModel.Indeling s in indelingList)
{
ListViewItem la = new ListViewItem("Kamer nummer: "+(s.KamerNummer).ToString());
listView1.Items.Add(la);
ListViewItem lb = new ListViewItem("Type kamer: "+(s.TypeKamer).ToString());
listView1.Items.Add(lb);
ListViewItem lc = new ListViewItem("Plekken: "+(s.AantalBedden).ToString());
listView1.Items.Add(lc);
listView1.Height = (listView1.Items.Count * 19);
tableLayoutPanel1.Controls.Add(listView1, x, y);
listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
}
counter2++;
}
}
}

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;
}

how can i get checkbox value after clicking button

checkbox are created dynamically.
protected void Page_Load(object sender, EventArgs e)
{
XDocument doc = XDocument.Load(#"C:\Users\Faraz\Documents\Visual Studio 2015\Projects\Assignment_3_i130316\Assignment_3_i130316\bin\Products.xml");
var goals = doc.Element("FTSRecord").Elements("Approval_PickDrop");
var array = goals.Select(x => x.Value).ToArray();
int rowCnt = 0;
int rowCtr;
foreach (var b in array)
{
rowCnt++;
}
for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
{
TableRow tRow = new TableRow();
Table1.Rows.Add(tRow);
for (int cellCtr = 1; cellCtr <= 2; cellCtr++)
{
// Create a new cell and add it to the row.
TableCell tCell = new TableCell();
tRow.Cells.Add(tCell);
if (cellCtr == 1)
{
tCell.Text = array[rowCtr - 1];
tRow.Cells.Add(tCell);
}
else
{
CheckBox c = new CheckBox();
c.ID = "ID" + cellCtr;
tCell.Controls.Add(c);
}
}
}
}
Use Request.Form with the name of your checkbox
https://msdn.microsoft.com/en-us/library/ms525985(v=vs.90).aspx
Change Checkbox cell ID :: c.ID = "ID_" + rowCtr;
After click on button you get Table1 and iterate each row and get checkbox cell value for each row on respect of its ID c.ID = "ID_" + rowCtr; split Id with'_' get its array index position and the checkbox checked status tells its value.

How to add a dynamically link in a dynamically added table

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);
}

Categories

Resources