?
Solved

Tyring to dynamically add and delete rows in an asp.net table.

Posted on 2009-02-17
6
Medium Priority
?
252 Views
Last Modified: 2013-12-17
Using C# and asp.net. I have a table with ten dynamically added rows each containing values 0 through to 9.

I want to try to be able to add a row to the top containing new contents and then delete from the bottom everytime I click a button. This only happens the first time at present. Although the event handler does still execute on the second click.

 I am using dateTime object for the example. When button is first clicked the current dateTime is added and the last row is removed, in this instance the value 9. So everything is moved up successfully. On the second attempt this same procedure is expected and desired, though it is not performed. What happens is the dateTime is updated in the first row and thats is. What I am trying to get is for everything to be moved up one row. Therefore there would be two dateTimes displayed in the first two columns and then initial row 8 and 9 will have been removed.

Code example below, can do this in Javascript, however I would like to try to achieve the same effect on server side to reduce overheads. Any help would be greatly appreciated.
Here is aspx code:
 
<asp:Table ID="tblTemp" runat="server" CssClass="myTable"></asp:Table>   
<asp:Button ID="Button1" runat="server" Text="Click Me!" OnClick="btnClick" CssClass="myButton"/>
 
Here is the aspx.cs code behind:
 
 protected void Page_Load(object sender, EventArgs e)
    {
        int counter = 0;
        while (counter <= 9)
        {
            TableRow trRow = new TableRow();
            TableCell tcCell = new TableCell();
            tcCell.CssClass = "myCells";
            
 
            tcCell.Text = counter.ToString();
            trRow.Controls.Add(tcCell);
 
            tblTemp.Controls.Add(trRow);
            counter += 1;
        }
    }
 
    protected void btnClick(object sender, EventArgs e)
    {
        DateTime current = DateTime.Now;
 
        TableRow trRow = new TableRow();
        TableCell tcCell = new TableCell();
        tcCell.CssClass = "myCells";
        tcCell.Text = current.ToString();
        trRow.Controls.Add(tcCell);
        tblTemp.Controls.AddAt(0, trRow);
        tblTemp.Controls.RemoveAt(9);
    }

Open in new window

0
Comment
Question by:metta0_3
  • 4
  • 2
6 Comments
 
LVL 11

Expert Comment

by:jmwheeler
ID: 23659406
I think all you have to do is change your last two lines to (check order):

tblTemp.Rows.RemoveAt(9);
tblTemp.Rows.AddAt(0, trRow);

0
 
LVL 11

Expert Comment

by:jmwheeler
ID: 23659420
In your original code you were adding the row first which would change your indexes to 0-10 because you now have eleven rows.  So you can change the order of the statements or change your RemoveAt call to RemoveAt(10)
0
 

Author Comment

by:metta0_3
ID: 23659604
Thanks for the advice. That was an oversight on my part. However, it does not resolve the issue. If I click the button a second time it does not it does not push everything up once again. I'm trying to make continuous so that eveytime button is clicked rows are shifted up one and last one removed and new one added at the front.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Accepted Solution

by:
jmwheeler earned 2000 total points
ID: 23660232
I think your problem may be your Page_Load code.  Try wrapping everything in an if statement

if (!IsPostBack)
{
     ....Your page_load code
}
0
 
LVL 11

Expert Comment

by:jmwheeler
ID: 23660242
You need to check IsPostBack if you don't want your table reset on every post back, only the first time the page is loaded.
0
 

Author Closing Comment

by:metta0_3
ID: 31547729
Thanks you a star! Resolved it.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question