Dynamic table help

dynamic table isnt retaining

Page 1 of 1

5 Replies - 7774 Views - Last Post: 13 October 2010 - 06:36 AM Rate Topic: -----

#1 knightmare2dream  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 13-February 10

Dynamic table help

Posted 08 October 2010 - 11:29 AM

Have a problem, I have an asp:Table control that I have made dynamic based on a users choice from a drop down, then information contained in a database. The rows and cells are populated with controls and static text from whats needed. The table's cells and rows are added in the dropdown's selected index change event. Problem is when I perform any action on the page after that the table has no controls, rows, etc..
Any idea what I'm missing? There is nothing in my code behind that I could see could cause this. Here is some of the code, cant put alot in cause theres alot:
 protected void siteList_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            Table1.Rows.Clear();
            TableRow headerRow = new TableRow();
            TableHeaderCell dayHead = new TableHeaderCell();
            TableHeaderCell empHead = new TableHeaderCell();
            TableHeaderCell startHead = new TableHeaderCell();
            TableHeaderCell endHead = new TableHeaderCell();
            dayHead.Controls.Add(new LiteralControl("Day"));
            empHead.Controls.Add(new LiteralControl("Employee"));
            startHead.Controls.Add(new LiteralControl("Start Time"));
            endHead.Controls.Add(new LiteralControl("End Time"));
            headerRow.Cells.Add(dayHead);
            headerRow.Cells.Add(empHead);
            headerRow.Cells.Add(startHead);
            headerRow.Cells.Add(endHead);
            Table1.Rows.Add(headerRow);

            EmpInfoDataContext db = new EmpInfoDataContext();
            var sql = from x in db.Sites
                      where x.SiteName.Equals(siteList.SelectedValue)
                      select x;
            foreach (var site in sql)
            {
                int req = 0;
                if (site.SunReqEmp != 0)
                {
                    req = site.SunReqEmp;
                    TableRow sunHeadRow = new TableRow();
                    TableHeaderCell sunHead = new TableHeaderCell();
                    sunHead.Controls.Add(new LiteralControl("Sunday"));
                    sunHeadRow.Cells.Add(sunHead);


                    for (int i = 0; i < req; i++)
                    {
                        if (i == 0)
                        {
                            TableCell empNameCell = new TableCell();
                            DropDownList dynamicddl = new DropDownList();
                            dynamicddl.ID = "sunDropDownList" + i;
                            bindListWithEmployees(dynamicddl);
                            empNameCell.Controls.Add(dynamicddl);
                            TableCell startTimeCell = new TableCell();
                            TimeDropDown startddl = new TimeDropDown("sunstart" + i);
                            startTimeCell.Controls.Add(startddl);
                            TableCell endTimeCell = new TableCell();
                            TimeDropDown endddl = new TimeDropDown("sunend" + i);
                            endTimeCell.Controls.Add(endddl);

                            sunHeadRow.Cells.Add(empNameCell);
                            sunHeadRow.Cells.Add(startTimeCell);
                            sunHeadRow.Cells.Add(endTimeCell);

                            
                            Table1.Rows.Add(sunHeadRow);
                        }
else
                        {
                            TableRow tempRow = new TableRow();
                            TableCell empNameCell = new TableCell();
                            DropDownList dynamicddl = new DropDownList();
                            dynamicddl.ID = "sunDropDownList" + i;
                            bindListWithEmployees(dynamicddl);
                            empNameCell.Controls.Add(dynamicddl);
                            TableCell startTimeCell = new TableCell();
                            TimeDropDown startddl = new TimeDropDown("sunstart" + i);
                            startTimeCell.Controls.Add(startddl);
                            TableCell endTimeCell = new TableCell();
                            TimeDropDown endddl = new TimeDropDown("sunend" + i);
                            endTimeCell.Controls.Add(endddl);

                            TableCell emptyCell = new TableCell();
                            tempRow.Cells.Add(emptyCell);
                            tempRow.Cells.Add(empNameCell);
                            tempRow.Cells.Add(startTimeCell);
                            tempRow.Cells.Add(endTimeCell);

                            if ((i % 2) == 0)
                            {
                                //tempRow.BackColor = Color.FromArgb(15199485);
                            }
                            Table1.Rows.Add(tempRow);
                        }

                    }//end of For statement



Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic table help

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Dynamic table help

Posted 08 October 2010 - 11:59 AM

You are going to need to rebuild the table each time your page posts back.
Was This Post Helpful? 1
  • +
  • -

#3 knightmare2dream  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 13-February 10

Re: Dynamic table help

Posted 08 October 2010 - 01:21 PM

Thank you, I did that and now the Table retains its data. However I have another question, what is a way to access the dropdownlist's values in the cells. I tried using the FindControl method to assign it to a generic control first, but it cant find the control. I apologize for the noobness but I am fairly new to asp.net but am familiar with c#
Was This Post Helpful? 0
  • +
  • -

#4 knightmare2dream  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 13-February 10

Re: Dynamic table help

Posted 08 October 2010 - 01:31 PM

Thank you for ur help, I was able to solve the issue with your advice.
Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Dynamic table help

Posted 08 October 2010 - 01:45 PM

View Postknightmare2dream, on 08 October 2010 - 12:21 PM, said:

Thank you, I did that and now the Table retains its data. However I have another question, what is a way to access the dropdownlist's values in the cells. I tried using the FindControl method to assign it to a generic control first, but it cant find the control. I apologize for the noobness but I am fairly new to asp.net but am familiar with c#

You are going to need to get a reference to the Row of the Table that contains the DropDownList and then do a Row.FindControl to get a reference to the DropDownList.
Was This Post Helpful? 0
  • +
  • -

#6 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Dynamic table help

Posted 13 October 2010 - 06:36 AM

Instead of recreating the table every page load, I recommend storing it in cache or session so that it is more efficient. Update the table in cache/session just when it needs to be changed...otherwise just retrieve the table from cache/session and use it (instead of recreating it every time).

-Frinny
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1