Everytime I click on a button I want to add a tableRow to a table like this:
TextBox TB = new TextBox();
TableCell Cell = new TableCell();
Cell.Controls.Add(TB);
TableRow Row = new TableRow();
Row.Cells.Add(Cell);
table.Rows.Add(Row);It works the first time, but when I click the button again the last row is overwritten. Why, and how can I solve this? I want an easy way for the user to add more rows to a table.
![]() |
0 |
![]() |
void btn_Click(sender, e) { TextBox TB = new TextBox(); TableCell Cell = new TableCell(); Cell.Controls.Add(TB); TableRow Row = new TableRow(); Row.Cells.Add(Cell); table.Rows.Add(Row); int rows = 0; if (ViewState["rows] != null) rows = (int)ViewState["rows]; rows++; ViewState["rows] = rows; } void Page_Load(sender, e) { if (ViewState["rows] != null) { int rows = (int)ViewState["rows]; for (i = 1; i <= rows; i++) { // Create the textbox and add it to the row } } }Golden rule about dynamic controls (controls that you add at runtime) is that you have to recreate them on each and every postback. You somehow has to track (viewstate, hidden fields, session etc) how many rows have been added and add those on each page load.
![]() |
0 |
![]() |
Thanks for your help! Now I understand what I need to do.
The thing is that I can't just add new rows, because the user can have written things in the textbox. So I must somehow get the old rows the user has added and then add the new row. How can I do that?
![]() |
0 |
![]() |
The user entered values in text boxs will stay there.... as page postback processing will take place after page Load.... by the time it tries to renders values from form processing, you already created the required rows in load event....... try yourself.....
Mark it as answer if you really think it helped you a bit.
Bob Lakkakula
http://nlakkakula.wordpress.com
![]() |
0 |
![]() |
Thanks for your help!
I mean the textbox in the row that was created by the user, and as it will be overwritten it can't stay there or can it?
![]() |
0 |
![]() |
I was talking about the textbox in dynamically created row too.... :-)
Mark it as answer if you really think it helped you a bit.
Bob Lakkakula
http://nlakkakula.wordpress.com
![]() |
0 |
![]() |
You just have to recreate the controls and viewstate will take care of the postback data for you (that is if you create the controls before or on page_load event). As the previous poster has written asp.net take cares of the rest. Try it yourself.
![]() |
0 |
![]() |
Thanks very much for your help! I finally got it working, my mistake!
Now I can add and delete rows. I now want the user to click on a button to save the values in the textboxes inside the table. I'm trying to do this with this code but it doesn't seem to be working. It just takes the value of the first tablerow, but it takes this value as many times as there are rows in the table.
foreach (TableRow row in table.Rows)
{
tb = row.Cells[0].FindControl("myTB") as TextBox;
saveData(tb);
}
I think I'm doing the same mistake as before, but I don't know how to fix it.Please help!
![]() |
0 |
![]() |
What is the signature of your method saveData (I mean what kind of parameter does it accept)? It might be better to send the Text inside the TextBox than sending the TextBox itself. Post your saveData method here.
![]() |
0 |
![]() |
I run this code when the insert command have been executed on my formview:
protected void FV_X_Inserted(object sender, FormViewInsertedEventArgs e)
{
TextBox tb;
Table table = FV_X.FindControl("Table") as Table;
string sqlcommand = "";
foreach (TableRow row in table.Rows)
{
tb = row.Cells[0].FindControl("TB") as TextBox;
sqlcommand += " INSERT INTO tbl (text) VALUES ('" + tb.Text + "'); ";
}
MySqlConnection Conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["x"].ConnectionString);
MySqlCommand Cmd = new MySqlCommand(sqlcommand, Conn);
Cmd.Connection.Open();
Cmd.ExecuteNonQuery();
Cmd.Connection.Close();
Cmd.Dispose();
Conn.Dispose();}
![]() |
0 |
![]() |
Put a breakpoint in your code just after the foreach loop and observe the sqlcommand variable. Check what is the value and if you have physical access to the Query Analyzer then run the query? Does it run fine?
![]() |
0 |
![]() |
Hi again!
Yes, I tried this. It has added the querys but it only contains the value from the first TextBox in every query, even if I loop throught all cells. What can be wrong?
![]() |
0 |
![]() |
Anyone?
![]() |
0 |
![]() |
I found it out myself I had to set TextBox.ID to different ID for every TextBox.
![]() |
0 |
![]() |