Add controls to a html table in code behind - c#

I have a table column name called QUESTIONTEXT where there are 3 questions in that column. so, there's 3 rows of questions. I am adding those questions to the first column which is working like a charm. Now, i want to add a text box or a checkbox or radio button either next to it in the same cell or to the next column in the same row.
I have 1 question.
1: How to add a control to either in the same row as the text i am currently adding or to the next column in the same row in the table?.
SCENARIO: In the same table i have the column QUESTIONTEXT i also have a TYPEID column with numeric data. Either a 1 or a 2
the idea is that i will know what control to generate for that specific question by the TYPEID number
1 for text
2 for checkbox
I can grab the number like this:
if (dataRow["TYPEID"].ToString() == "1")
So, for the first one its a 1 and i want to add a text box. I just need to generate a text box or checkbox.
Here is how i am currently adding the questions to the first column in the table.
TableRow tableRow;
TableCell tableCell;
foreach (DataTable dataTable in ds.Tables)
{
foreach (DataRow dataRow in dataTable.Rows)
{
tableRow = new TableRow();
tableCell = new TableCell();
TableRow tableRow2 = new TableRow();
TableCell tableCel2 = new TableCell();
tableCell.Text = dataRow["QUESTIONTEXT"].ToString();
if (dataRow["TYPEID"].ToString() == "1")
tableRow.Cells.Add(tableCell);
myTable.Rows.Add(tableRow);
}
}
I also created a enum to aid in the process but not sure if it will help!
public enum typeID : byte
{
text = 1,
multiple_choice = 2
};
Cant someone help? please!

try this for adding second column in same row
TableRow tableRow;
TableCell tableCell;
TableCell tableCell2;
foreach (DataTable dataTable in ds.Tables)
{
foreach (DataRow dataRow in dataTable.Rows)
{
tableRow = new TableRow();
tableCell = new TableCell();
tableCell.Text = dataRow["QUESTIONTEXT"].ToString();
tableCell2 = new TableCell();
switch (dataRow["TYPEID"].ToString())
{
case "1":
Label lbl = new Label();
lbl.Text = "";
tableCell2.Controls.Add(lbl);
break;
case "2":
CheckBox chk = new CheckBox();
chk.Text = "";
tableCell2.Controls.Add(chk);
break;
}
tableRow.Cells.Add(tableCell);
tableRow.Cells.Add(tableCell2);
myTable.Rows.Add(tableRow);
}
}

Related

The code only creates the last checkbox, what should I do differently or add?

I want to create checkboxes dynamically according to the amount of nozzles linked to a site.
Table table = new Table();
List<string> check = Helpers.getNozzle(Selected.SelectedValue);
//create a new row, cell and checkbox
TableRow row = new TableRow();
TableCell cell = new TableCell();
CheckBox cb = new CheckBox();
foreach (var item in check)
{
//set some checkbox properties
cb.Text = "Nozzle " + item;
//add the checkbox to the cell
cell.Controls.Add(cb);
//the cell to the row
row.Controls.Add(cell);
}
//and the row to the table
table.Controls.Add(row);
//finally add the table to the page
controleplaceholder.Controls.Add(table);
both the foreach and for loops only create the last check box. my question is what am I missing and why it is only creating the last checkbox.
here is the aspx side
<tr style="color: white">
<td>Nozzle(s):
</td>
<td id="checkboxes" runat="server">
<asp:PlaceHolder runat="server" ID="controleplaceholder" />
</td>
</tr>
Move TableCell cell = new TableCell(); & CheckBox cb = new CheckBox(); inside foreach because as you are only renaming and adding the same object again into row.Controls it will only add single control.
foreach (var item in check)
{
TableCell cell = new TableCell();
CheckBox cb = new CheckBox();
//set some checkbox properties
cb.Text = "Nozzle " + item;
//add the checkbox to the cell
cell.Controls.Add(cb);
//the cell to the row
row.Controls.Add(cell);
}
Can you try just each time cb = new CheckBox(); in foreach loop?
foreach (var item in check)
{
cb = new CheckBox();
//set some checkbox properties
cb.Text = "Nozzle " + item;
//add the checkbox to the cell
cell.Controls.Add(cb);
//the cell to the row
row.Controls.Add(cell);
}
You are trying to add just 1 row to your table. Try this;
Table table = new Table();
List<string> check = Helpers.getNozzle(Selected.SelectedValue);
foreach (var item in check)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
CheckBox cb = new CheckBox();
cell.Controls.Add(cb);
cb.Text = "Nozzle " + item;
row.Controls.Add(cell);
table.Controls.Add(row);
}
controleplaceholder.Controls.Add(table);
Lets look your old code, when you create cell, row and checkbox items outside of foreach loop, you are create just one row, cell and checkbox. Becouse of this, when you try to show your table, you see just one row and cell. But if you create in foreach loop and push the table every item, you can see one row for each list value.

Dynamically choosing what row data is added to a table

Adding a table dynamically in code behind I can do something like...
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
cell1.Text = "blah blah blah";
row.Cells.Add(cell1);
myTable.Rows.Add(row);
but what if the order of the table rows depends on the user input....so for each iteration it is either going to be A or B. If it is A, extend row a...If it is B, extend row b
after 1st iteration
A: 123
after 2nd iteration
A: 123
B: abc
after 3rd iteration
A: 123456
B abc
after 4th iteration
A: 123456789
B: abc
Ive tried rows.addAt(index, row); but its not working says index is out of range. is this even the correct way to do this? thanks for any replies
//forloop
{
if(A)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
cell1.Text = "blah blah blah";
row.Cells.Add(cell1);
myTable.Rows.AddAt(0, row);
}
if(B)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
cell1.Text = "blah blah blah";
row.Cells.Add(cell1);
myTable.Rows.Add(1, row);
}
One problem I can see is that each time you are creating a new row/cell instead of getting the reference to an existing row/cell. If I understand correctly, your table has 2 rows, each with 1 cell, and you're trying to grow the contents?
You need to do something more like this. Note that I have not tested this:
TableCell existingCell = myTable.Rows[0].Cells[0];
existingCell.Text += "NewData";
Or if you wanted to add new cells to the existing rows it would be similar:
TableRow existingRow = myTable.Rows[0];
TableCell newCell = new TableCell();
newCell.Text = "NewData";
existingRow.add(newCell);

Get values from TextBox

I have two functions. In my first i have created the TextBox:
private TableRow GetGebuchteDienstleistungRow(Dienstleistungsreservierung dr, int rowIndex)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
if (dr.Dienstleistung.Mengeneinheit == Mengeneinheit.Basket)
{
foreach (Basketdienstleistung basketDl in dr.Dienstleistung.Basket)
{
cell = new TableCell();
cell.Text = "<tr />";
row.Cells.Add(cell);
TableRenderer.AddTableCell(row, dr.Von.ToString(form.DatumsFormat), 1);
TableRenderer.AddTableCell(row, dr.Von.ToString(form.ZeitFormat), 1);
TableRenderer.AddTableCell(row, dr.Bis.ToString(form.DatumsFormat), 1);
TableRenderer.AddTableCell(row, dr.Bis.ToString(form.ZeitFormat), 1);
cell = new TableCell();
var txt = new TextBox();
txt.Text = string.Format("{0:0.00}", dr.Bestellmenge * basketDl.Anzahl);
txt.Width = 42;
txt.MaxLength = 5;
txt.CssClass = "nummer";
cell.Controls.Add(txt);
row.Cells.Add(cell);
................
}
}
}
.................
How can i get the value of the created TextBox in my second function? Specifically I want to pass the value in a database.
You can use FindControl method
var textBox = (TextBox)cell.FindControl("YourID");
You can enumerate through all controls in specific cell and find your textbox by name
cell.Controls.OfType<TextBox>();

Table generation from Code Behind

I know what my fault is, but not sure how to resolve. I am trying to generate an asp:table from Code Behind.
The table should be 3 cells wide... I'll work on the row limit later.
Here's my code:
GallaryImage g = new GallaryImage();
var images = g.GetAll();
photos.Style.Add("width","100%");
photos.Style.Add("border-style","none");
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tr.Cells.Add(tc);
tr.Cells.Add(tc);
tr.Cells.Add(tc);
int cntr = 0;
TableRow row = new TableRow();
foreach (var image in images)
{
cntr++;
TableCell cell = new TableCell();
Image i = new Image();
i.ImageUrl = image.fullThumbPath;
cell.Controls.Add(i);
row.Cells.Add(cell);
if(cntr%3==0)
{
photos.Rows.Add(row);
row.Cells.Clear();
}
}
if(row.Cells.Count > 0)
photos.Rows.Add(row);
}
My problem is that I need to create a new row in the Foreach, only when I need the new row... i.e, when we have added 3 cells.
I thought I could add the row to the table, and then clear the row to start a new row - but that's not working, as I just keep clearing the same row object... and therefore, never add multiple rows.
Can someone assist with my logic here?
GallaryImage g = new GallaryImage();
var images = g.GetAll();
photos.Style.Add("width","100%");
photos.Style.Add("border-style","none");
int cntr = 0;
TableRow row = new TableRow();
foreach (var image in images)
{
cntr++;
TableCell cell = new TableCell();
Image i = new Image();
i.ImageUrl = image.fullThumbPath;
cell.Controls.Add(i);
row.Cells.Add(cell);
if(cntr%3==0)
{
photos.Rows.Add(row);
row = new TableRow();
}
}
if(row.Cells.Count > 0)
photos.Rows.Add(row);
}

Can't add duplicate row to the asp.net Table Control

I have an asp.net table control like this:
TableHeader
A Text | Textbox
What I want to do is, in the page_load event, duplicate second row with all the controls within it, change the text in the first cell and add as a new row. So here is my code:
for (int i = 0; i < loop1counter; i++)
{
TableRow row = new TableRow();
row = myTable.Rows[1]; //Duplicate original row
char c = (char)(66 + i);
if (c != 'M')
{
row.Cells[0].Text = c.ToString();
myTable.Rows.Add(row);
}
}
But when I execute this code it justs overwrites on the original row and row count of the table doesn't change. Thanks for help....
As thekip mentioned, you are re-writing the reference.
Create a new row. Add it to the grid and then copy the cell values in whatever manner you want.
Something like:
TableRow tRow = new TableRow();
myTable.Rows.Add(tRow);
foreach (TableCell cell in myTable.Rows[1].Cells)
{
TableCell tCell = new TableCell();
tCell.Text = cell.Text;
tRow.Cells.Add(tCell);
}
It gets overwritten because you overwrite the reference. You don't do a copy, essentially the row = new TableRow() is doing nothing.
You should use
myTable.ImportRow(myTable.Rows[1]).
Adjusted based on response try:
row = myTable.Rows[1].MemberwiseClone();
so try this
private TableRow CopyTableRow(TableRow row)
{
TableRow newRow = new TableRow();
foreach (TableCell cell in row.Cells)
{
TableCell tempCell = new TableCell();
foreach (Control ctrl in cell.Controls)
{
tempCell.Controls.Add(ctrl);
}
tempCell.Text = cell.Text;
newRow.Cells.Add(tempCell);
}
return newRow;
}
your code:
for (int i = 0; i < loop1counter; i++)
{
TableRow row = CopyTableRow(myTable.Rows[1]); //Duplicate original row
char c = (char)(66 + i);
if (c != 'M')
{
row.Cells[0].Text = c.ToString();
myTable.Rows.Add(row);
}
}

Categories