Trying to append (add) row to table: row overwritten :(

 Hi,

I was hoping someone could lend me a hand. I have a button and want to add one row to a table every time I click that button. The thing is, it works find the first time, but the second, third, ... the row is simply overwritten. Does anyone know what I'm doing wrong? I simply want to append a new row to the end of the table. This is the C# code:

1        protected void Button1_Click(object sender, EventArgs e)
2        {
3            TableRow row = new TableRow();
4    
5            for (int i = 0; i < 5; ++i)
6            {
7                TableCell cell = new TableCell();
8                cell.Controls.Add(new LiteralControl(Guid.NewGuid().ToString()));
9                row.Cells.Add(cell);
10           }
11   
12           Table1.Rows.Add(row);
13       }
14   

 Thanks in advance.

0
fahlen
1/4/2007 4:32:19 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

9 Replies
808 Views

Similar Articles

[PageSpeed] 11

hi,

that's coz the page is stateless, the first time you add the new row, it works ok, then, the page post back, and after it,

the page forget all about the new row added to the table, so when you add a new row the second time,

it only displayed one row not two as you expected.

one method is store the table in the session object

 protected void b1_Click(object sender, EventArgs e)
    {
        if (Session["table"] == null)
        {
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            tc.Text = "abc";
            tr.Cells.Add(tc);
            Table1.Rows.Add(tr);
            Session["table"] = Table1;
        }
        else
        {
            Table t= (Table)Session["table"];
            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            tc.Text = "abc";
            tr.Cells.Add(tc);
            t.Rows.Add(tr);
           form1.Controls.Add(t);
           Session["table"] = t;
        }
    }


Jessica Cao
Sincerely,
Microsoft Online Community Support


“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
0
Jessica
1/5/2007 3:01:34 AM
try enabling the viewstate of the table like the code below. i'm not sure it will work but i believe that the use of the property enableviewstate 
<table enableviewstate="true" > </table>
0
ulysses098
1/5/2007 3:28:26 AM

hi,

It cannot be implemented by just adding eableViewState="true"

thx

jessica


Jessica Cao
Sincerely,
Microsoft Online Community Support


“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
0
Jessica
1/5/2007 4:21:13 AM
aaah, thank for the correction. then what does enableviewstate do? i thought it will behave just like other server control if you set to true the enableviewstate.
0
ulysses098
1/5/2007 4:38:43 AM

the enableview state help the control to retain the value populated at runtime. for instance, if you populated your DDL in the

if(!IsPostBack)

{ populate your DDL 

}

then each time the page postback, the DDL doesn't lose the value.That's coz the EnableViewState=true help the DDL retain the value populate when the page first load.

but you add the table row at runtime, the row is a part of the table control, not data stored in the table,so the EnableViewState doesn't take effect in this situation.

hope it helps


Jessica Cao
Sincerely,
Microsoft Online Community Support


“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
0
Jessica
1/5/2007 5:08:58 AM
yes it really help. I have another question. what do you mean by the data stored in the table? is it the text inside the table?
0
ulysses098
1/5/2007 7:17:33 AM
Thanks Jessica. Now it works fine Smile
0
fahlen
1/5/2007 8:10:26 AM

hi ulysses098,

I just mean like this you add the table cell's text in the first time the page load

  if (!IsPostBack)
        {
            Table1.Rows[0].Cells[1].Text = "abc";
        }

and then each time the page postbacks, the table will retain the text value

if you set enableViewState= false, then the table will lose the text after postback

hope it helps

jessica


Jessica Cao
Sincerely,
Microsoft Online Community Support


“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
0
Jessica
1/5/2007 8:52:15 AM

Hi jessica,


Thanks to the responds. I have clearly understand now. thank you very much on answering my question.

ulysses098 

0
ulysses098
1/5/2007 9:06:00 AM
Reply:

Similar Artilces:

Add row to table with table.row.add()
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 r...

How to add new rows in the form of rows
Hi, I can able to insert,delete and update record in gridview but cannot able to insert any new rows in the gird. In windows application we can insert new records(using DataGridView) into the database but not in web page. How to insert the records in gridview like control. Please tell me how to insert records in database in the form of rows. I mean i want to enter 20 records at one time.   Thanks.   Regards ArshadPlease Mark as Answer if the post is helpful to you. hi, Have you set the property AllowUserToAddRows=True; for the datagridview?????  If you are us...

Is having a trigger that inserts a row in Table 'A', when a row in same table is inserted by ADo.Net code?
I want to insert a row for a Global user  in Table 'A' whenever ADO.Net code inserts a Local user row into same table. I recommended using a trigger to implement this functionality, but the DBA was against it, saying that stored proecedures should be used, since triggers are unreliable and slow down the system by placing unecessary locks on the table. Is this true OR the DBA is saying something wrong? My thinking is that Microsoft will never include triggers if they are unreliable and the DBA is just wanting to offload the extra DBA task of triggers to the programmer so that a s...

How to add a footer row to a table after deleting all footer rows
I'm sure this is a very easy thing to do, but I can't figure out how to do it. I have a table, and I deleted all the footer rows.  Now I would like to add one footer row.  How do I do that? If I right-click on the detail row and select Insert Row Below, it is, of course, another detail row. I'm stumped -- thanks for any help you can give!Cynthia Do you have groups in your table?  You should be able to right click on any table row and toggle the corresponding menu item to show the table header, details or footer.  It is in the right click menu includ...

ADO.net problem: i can not view image data for the rest of my table rows except the image for the first row but i can read all the other non image fields for all rows.
Hi i have a table with the following fields imgid............int imgdata........image imgtitle.........varchar(max) imgType........varchar(max) imgLength.....bigint incident_id.....int i have no problem with uploading the image but viewing the uploaded image(s) in a gridview has caused me great pain. The probelm is i have two pages. page1 grabs the image to display and page2 has a gridview that displays the grabed image. I wanted to have an sql statement like SELECT * FROM mytable WHERE incident_id=someValue. IF i have 5 records in this table and i view page2 ...

I want to select top 3 rows except the first from 4 rows of a Table. It should select from top 2 row,top3 row and top4 row.
 Hi, Let say I've 4 rows in a table. I want to select top 3 rows except the first from 4 rows of a Table. It should select from top 2 row,top3 row and top4 row. Thanks in advance Asghar Ali Mohammed http://www.aliwebdev.com(Web Designer and Developer)Do not forget to "MARK AS ANSWER" on the post that helped you. If you can use Linq, you can use Skip. http://msdn.microsoft.com/en-us/vcsharp/aa336757.aspxThanks, EdMicrosoft MVP - ASP/ASP.NET Hii dear..U can fire the Query select Top(3) from Table1 order by id desc.. "A conclusion is whe...

Update a rows in one table for each row in other table
Hello all, i am a newbie in SQL and i want to ask for your help in order to do the following update statement. I have a table tblUsers which holds all the users, one of the fields in their GroupId. In the UserGroup table i have a field "UsersCount" and in that field i want to keep the number of the users in that group. Basically i want everytime i am saving a user to recalculate the users in all usergroups. I am trying to have something like:SELECT User_GroupId, count(User_GroupId) as UsersCount FROM tblUsersWHERE DeletedFlag = 0 GROUP BY User_GroupId and then:"FOR EACH" User_GroupIdUPDAT...

Update row in Table A from row in Table B
Hello, I have 2 tables.... table A and table B. Ok, In a stored procedure, I pass in an int value @ID I want to get the row from Table B WHERE ID =@ID....and then UPDATE the Row in Table A that has the same ID...with the values from table B. Table A ID | Col1 | Col2 ----------------------- 2 a a Table B ID | Col1 | Col2 ----------------------- 2 b b I would like to update A.Col1 with B.Col1 and A.Col2 with B.Col2 Can someone help me out with this syntax? Thanks a lot. update tableA ...

add data from multiple rows in table to one row in gridview
 heya is there any way of adding data from multiple rows in table to one row in gridview. eg. ipc                 column 1 ,            2 ,                       3,                 4  1,    11111                 33                  &nbs...

how to compare rows in the datatable and remove the duplicate rows (before removal of duplicate row i need to append those values to original rows)
Hii.             I have a datatable in that i have some rows.. i need to compare those rows one among the other based on first column only. If its duplicate row i need to append those col values to original row. For example here is the data in the table.: Total - 13 columns Name:     Jan:   Feb:   March:...............................Dec  Phani      2        4          ...

Trying to add a new table row on Page_Load
Hello, I am running the following code below in an attempt to loop thru the results of a query in order to populate a table full of html. The code is below: ---------------------------- Do While (myReader.Read()) FieldOrder.InnerHtml = myReader("FieldOrder") & ")" NameOfField.InnerHtml = myReader("FieldPrompt") FieldValue.InnerHtml = myReader("FieldPrompt") chkbox.ID = "chkbox" & myReader("FieldOrder") txtbox....

Error appears: "This row already belongs to this table." on add row to a datatable
The error message "This row already belongs to this table." appears when I attempt to add a 2nd row to a datatable.  The code is as follows: ==================================================================  Dim dtTrans As New DataTable 'Set up datatable columns dtTrans.Columns.Add(New DataColumn("EventDate", Type.GetType("System.DateTime"))) dtTrans.Columns.Add(New DataColumn("PostDate", Type.GetType("System.DateTime"))) dtTrans.Columns.Add(New DataColumn("PostCode", Type.GetType("System.String"))) dtTrans.Columns.Add(New DataColumn("PostCodeDesc", Type.GetType("System.S...

Help with "add a row" for a web form
I am trying to a one-to-many form that allows the user to ADD A ROW for data input.I'm doing this by updating a LABEL text as the last row of a table. I want to add a PostBack on one of these fields so it will update the values if someone changes a quantity. I tried doing a VIEW SOURCE of a rendered page, then copying that into the page.. but it no workie :( A can someone suggest how I can get this done? protected void addrow(object sender, EventArgs e) { int iNext = Convert.ToInt32(lblRowCount.Text); lblRowCount.Text = iNext.ToString(); ...

appending tables rows to other tables in griview..
hi frinds I have two tables1 & table2 the table2 is having 10 rows with sorted list table 1 having 1 rows without sorted list.. so i am append table2 values unders the table1 row.. I did table 2 sorting like this..DataView dv = dt1.DefaultView; dv.Sort = "LastName ASC";foreach (DataRow r in dt1.Rows) { dt.Rows.Add(r.ItemArray); this.GridView1.DataSource = dt;GridView1.DataBind(); } but when I am seeing in quickwatch in ItemArray I am not seeing dt1 in sorted order can any one tell me why I am not seeing sorted order that.. thanks asap.. Hi, Please try thisData...

Web resources about - Trying to append (add) row to table: row overwritten :( - asp.net.web-forms

Missing Malaysia Airlines plane: onboard flight computer manually overwritten
WASHINGTON - The first turn to the west that diverted the missing Malaysia Airlines plane from its planned flight path from Kuala Lumpur to Beijing ...

‘Crimson Peak’ Reviews: Jessica Chastain Is a ‘Fabulous Monster’ in an ‘Overwritten’ Horror Film
... Creepy Haunted Mansion Thrills But some reviews note that while the film boasts stellar sets, it features “overfamiliar elements,” is “overwritten” ...

Missing Plane MH370 Computer Was 'Manually Overwritten' 17
The flight computer on missing Malaysian Airlines flight MH370 was "manually overwritten", investigators have said. The plane carrying 239 passengers ...

Garbage collection (computer science) - Wikipedia, the free encyclopedia
In computer science , garbage collection ( GC ) is a form of automatic memory management . The garbage collector , or just collector , attempts ...

Version Comparison: Apple Support Communities
Apple's iTunes software is, in the main, designed to transfer data to their devices but not in the other direction. In general if you try to ...

Inside AirPrint: How to add iOS printer sharing back to Mac OS X
Experimenters have discovered the configuration files Apple removed from the public release of Mac OS X 10.6.5; adding them back restores the ...

301 Redirecting WordPress Posts
I just upgraded my WordPress installation to 2.2.1 – partly because a friend warned me of a fairly dangerous exploit in the previous version. ...

Baselux
Your hard disk is damaged, and there are important files in the disk. What would you do? Of course, it’s best to send your hard disk drive to ...

10. What is system bucket and what is its use in Amazon S3 operations?
System bucket is provided to backup all deleted file(s) and older version of overwritten file(s) on Amazon S3 in same structure.

The Dreaded Kernel Panic on Mac OS X
Apple's Mac OS X operating system is a flavor of Linux. So us Mac users have the privilege of rarely experiencing what are called "kernel panics." ...

Resources last updated: 11/26/2015 3:31:23 PM