Adding TableRows

K

Kevin Blount

I'd like to create a script that adds rows to an existing table, which
has a header and footer row. The new row should be added as the last
row BEFORE the footer.

Here's what I have, though it doesn't work as I require..

in the .aspx
Code:
01: <asp:Table ID="itemListTable" runat="server">
02:   <asp:TableHeaderRow ID="itemListTableHeader" runat="server" >
03:     <asp:TableCell runat="server">PROJECT</asp:TableCell>
04:     <asp:TableCell runat="server">ACTIVITY</asp:TableCell>
05:   </asp:TableHeaderRow>
06:
07:   <asp:TableFooterRow ID="itemListTableFooter" runat="server">
08:     <asp:TableCell runat="server" ColumnSpan="2"
HorizontalAlign="Right">TOTAL HOURS</asp:TableCell>
09:     <asp:TableCell runat="server" ><asp:Label ID="totals"
runat="server" ></asp:Label></asp:TableCell>
10:     <asp:TableCell runat="server" ></asp:TableCell>
11:   </asp:TableFooterRow>
12: </asp:Table>
13: <asp:Button ID="itemSubmit"  runat="server" Text="duh" />

in my .cs
Code:
01: protected void itemSubmit_Click(object sender, EventArgs e) {
02:   int rowCount = itemListTable.Rows.Count;
03:   TableRow newRow = new TableRow();
04:
05:   TableCell projectCell = new TableCell();
06:   projectCell.Text = itemProject.SelectedItem.Text;
07:   newRow.Cells.Add(projectCell);
08:
09:   TableCell activityCell = new TableCell();
10:   activityCell.Text = itemActivity.SelectedItem.Text;
11:   newRow.Cells.Add(activityCell);
12:
13:   itemListTable.Rows.AddAt((rowCount - 1), newRow);
14: }

The issue is that while this script does insert a TableRow before the
footer the first time, the second time I try to add a row, it
overwrites the first, i.e. the itemListTable.Rows.Count value does not
increase when I add a new row, so each time this method is called, the
row count is 2, the initial header and footer

I also tried other ideas, such as my own count of rows added, but
eventually my count goes outside of the acceptable values defined by
the RowsCollection max value (2).

Is there a way to either force an update of the Rows.Count for each
added row, or a better wya of doing this? The end result must be a
method of adding an indeterminate number of new rows, BEFORE the
footer and after the header.

Thanks in advance
 
K

Kevin Blount

Kevin,
This is really an ASP.NET newsgroup question, this is the C# language group
you've posted to. Try here:http://msdn.microsoft.com/newsgroups/default.aspx?pg=1&guid=&sloc=en-...

--Peter
"Inside every large program, there is a small program trying to get out."http://www.eggheadcafe.comhttp://petesbloggerama.blogspot.comhttp://www.blogmetafinder.com



Kevin Blount said:
I'd like to create a script that adds rows to an existing table, which
has a header and footer row. The new row should be added as the last
row BEFORE the footer.
Here's what I have, though it doesn't work as I require..
in the .aspx
Code:
01: <asp:Table ID="itemListTable" runat="server">
02:   <asp:TableHeaderRow ID="itemListTableHeader" runat="server" >
03:     <asp:TableCell runat="server">PROJECT</asp:TableCell>
04:     <asp:TableCell runat="server">ACTIVITY</asp:TableCell>
05:   </asp:TableHeaderRow>
06:
07:   <asp:TableFooterRow ID="itemListTableFooter" runat="server">
08:     <asp:TableCell runat="server" ColumnSpan="2"
HorizontalAlign="Right">TOTAL HOURS</asp:TableCell>
09:     <asp:TableCell runat="server" ><asp:Label ID="totals"
runat="server" ></asp:Label></asp:TableCell>
10:     <asp:TableCell runat="server" ></asp:TableCell>
11:   </asp:TableFooterRow>
12: </asp:Table>
13: <asp:Button ID="itemSubmit"  runat="server" Text="duh" />
in my .cs
Code:
01: protected void itemSubmit_Click(object sender, EventArgs e) {
02:   int rowCount = itemListTable.Rows.Count;
03:   TableRow newRow = new TableRow();
04:
05:   TableCell projectCell = new TableCell();
06:   projectCell.Text = itemProject.SelectedItem.Text;
07:   newRow.Cells.Add(projectCell);
08:
09:   TableCell activityCell = new TableCell();
10:   activityCell.Text = itemActivity.SelectedItem.Text;
11:   newRow.Cells.Add(activityCell);
12:
13:   itemListTable.Rows.AddAt((rowCount - 1), newRow);
14: }
The issue is that while this script does insert a TableRow before the
footer the first time, the second time I try to add a row, it
overwrites the first, i.e. the itemListTable.Rows.Count value does not
increase when I add a new row, so each time this method is called, the
row count is 2, the initial header and footer
I also tried other ideas, such as my own count of rows added, but
eventually my count goes outside of the acceptable values defined by
the RowsCollection max value (2).
Is there a way to either force an update of the Rows.Count for each
added row, or a better wya of doing this? The end result must be a
method of adding an indeterminate number of new rows, BEFORE the
footer and after the header.
Thanks in advance- Hide quoted text -

- Show quoted text -

As ASP.NET can be written using C# I'm not sure why you think I
shouldn't post this question here. I dubt everyone using this
newsgroup is Windows only...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top