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

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

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.

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

C# ASP.net Variable falls out of scope in if statment

I am having a schedule print from a database and using a loop. I have done this in asp but I am changing to c# asp.net and having troubles.
First I print the schedule headers
time|court|court|court
based on the number of courts then it prints the games.
Next ff the current records date is different the last date it will print the date over the entire table row.
Then it checks to see if the time is of the current record is the same as the last if it is not it prints the time and then the game record if it is it just prints the game record.
My problem is I am declaring the TableRow in the time if statment so when I try to use it in another statment it is out of scope. If I take the tablerow outside of the if statement it doesn't print right.
Here is what I have.
for (int i = 0; i < GameCount; i++)
{
DateTime currentdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i]["datetime"];
string ndate = currentdatetime.ToString("MM/dd/yyy");
string ntime = currentdatetime.ToString("HH:mm");
string nextdate = currentdatetime.ToString("MM/dd/yyy");
if (i + 1 != GameCount)
{
DateTime nextdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i + 1]["datetime"];
nextdate = nextdatetime.ToString("MM/dd/yyy");
}
string TeamA = Schedules.Tables["Schedule"].Rows[i]["teamA"].ToString();
string TeamB = Schedules.Tables["Schedule"].Rows[i]["teamB"].ToString();
//check to see if date is current
if (LastDate != ndate)
{
TableRow daterow = new TableRow();
TableCell datecell = new TableCell();
datecell.ColumnSpan = 7;
datecell.Controls.Add(new LiteralControl(ndate));
daterow.Cells.Add(datecell);
ScheduleTable.Rows.Add(daterow);
LastDate = ndate;
}
//print the games
if (currentdatetime != LastDateTime)
{
TableRow gamerow = new TableRow();
TableCell timecell = new TableCell();
timecell.Controls.Add(new LiteralControl(ntime));
gamerow.Cells.Add(timecell);
if (i + 1 != GameCount & ndate != nextdate)
{
ScheduleTable.Rows.Add(gamerow);
}
}//check to see if next game is part of the current row
else
{
TableCell gamecell = new TableCell();
gamecell.Controls.Add(new LiteralControl(TeamA + ".vs." + TeamB));
gamerow.Cells.Add(gamecell);
}
}
I can also post what I currently have in asp if that would help... you can go to www.swgc.ca/volleyball/2011/schedules.asp to see what I am trying to accomplish.
Thanks
Change your last bit to:
TableRow gamerow = new TableRow();
if (currentdatetime != LastDateTime)
{
TableCell timecell = new TableCell();
timecell.Controls.Add(new LiteralControl(ntime));
gamerow.Cells.Add(timecell);
}//check to see if next game is part of the current row
else
{
TableCell gamecell = new TableCell();
gamecell.Controls.Add(new LiteralControl(TeamA + ".vs." + TeamB));
gamerow.Cells.Add(gamecell);
}
if (i + 1 != GameCount & ndate != nextdate)
{
ScheduleTable.Rows.Add(gamerow);
}
And I'd strongly recommend looking at gridviews and repeater/list controls, as this is what they are for.
The easiest solution would be to pull your instantiation outside of the for loop. Try this (untested code):
TableRow gamerow = new TableRow();
TableCell timecell = new TableCell();
TableCell gamecell = new TableCell();
TableRow daterow = new TableRow();
TableCell datecell = new TableCell();
for (int i = 0; i < GameCount; i++)
{
DateTime currentdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i]["datetime"];
string ndate = currentdatetime.ToString("MM/dd/yyy");
string ntime = currentdatetime.ToString("HH:mm");
string nextdate = currentdatetime.ToString("MM/dd/yyy");
if (i + 1 != GameCount)
{
DateTime nextdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i + 1]["datetime"];
nextdate = nextdatetime.ToString("MM/dd/yyy");
}
string TeamA = Schedules.Tables["Schedule"].Rows[i]["teamA"].ToString();
string TeamB = Schedules.Tables["Schedule"].Rows[i]["teamB"].ToString();
//check to see if date is current
if (LastDate != ndate)
{
daterow = new TableRow();
datecell = new TableCell();
datecell.ColumnSpan = 7;
datecell.Controls.Add(new LiteralControl(ndate));
daterow.Cells.Add(datecell);
ScheduleTable.Rows.Add(daterow);
LastDate = ndate;
}
//print the games
if (currentdatetime != LastDateTime)
{
gamerow = new TableRow();
timecell = new TableCell();
timecell.Controls.Add(new LiteralControl(ntime));
gamerow.Cells.Add(timecell);
if (i + 1 != GameCount & ndate != nextdate)
{
ScheduleTable.Rows.Add(gamerow);
}
}//check to see if next game is part of the current row
else
{
gamecell = new TableCell();
gamecell.Controls.Add(new LiteralControl(TeamA + ".vs." + TeamB));
gamerow.Cells.Add(gamecell);
}
This is a non-optimized answer for your question. I feel like there is probably a better OO way to achieve your goal, but didn't want to answer a question you didn't ask.

Add controls to a html table in code behind

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

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

Categories