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