2
Answers

Problem with dynamically added Rows with buttons to Table

Terry

Terry

9y
698
1

Hi, 


    In my ASP.net web forms app, I have a drop down list & on selecting a value from it, I populate a Table. Table will have min of 3 rows & cols dynamic based on object selection. Here is my Table declaration in aspx :
        <asp:Table ID="floorTable" runat="server"  Width="100%" GridLines="Both">
        </asp:Table>


On selecting value from dropdown, I populate the table as follows :
        private void PopulateFloorRow(int floorNo, FloorPattern fp)
        {
            int cols = fp.UnitPattern.Count;

            // HEADER ROW
            TableRow thead = new TableRow(); 
            thead.Width = Unit.Percentage(100);
            thead.TableSection = TableRowSection.TableHeader;
            TableCell theadCell = new TableCell();
            theadCell.ColumnSpan = cols;
            Label title = new Label();
            title.Text = "Floor # " + floorNo;
            theadCell.Controls.Add(title);
            thead.Controls.Add(theadCell);

            TableRow planRow = GetFloorPlan(floorNo, fp);

            TableRow tr = new TableRow();
            TableCell tc = null;
            int tcWidPerc = (int)fp.UnitPattern.Count / 100;

            System.Diagnostics.Debug.WriteLine("UNIT PATTERNS OF FLOOR " + floorNo + " == " + fp.UnitPattern.Count());
            foreach (UnitPattern up in fp.UnitPattern)
            {
                
                tc = new TableCell();

                Button imgBtn = new Button();
                imgBtn.Attributes.Add("onClick", "UnitLinkClicked(this)");
                imgBtn.CommandArgument = up.UnitPatternId.ToString();
                imgBtn.Click += new EventHandler(UnitLinkClicked);
                imgBtn.BorderWidth = Unit.Pixel(10);
                imgBtn.BorderColor = Color.Transparent;
                imgBtn.Width = Unit.Percentage(100);
                if (up.UnitNo != null)
                {
                    imgBtn.Text = up.UnitNo;
                   // imgBtn.PostBackUrl = "~/Customers/Insert.aspx?projectId=" + selectedProject.ProjectId + ",unit=" + up.UnitNo;
                }
                if (up.Unit_Status == UnitStatus.Available)
                    imgBtn.BackColor = Color.Green;
                else if (up.Unit_Status == UnitStatus.Block)
                    imgBtn.BackColor = Color.Blue;
                else if (up.Unit_Status == UnitStatus.Sold)
                    imgBtn.BackColor = Color.Red;

                tc.Controls.Add(imgBtn);

                tr.Controls.Add(tc);
            }

            floorTable.Rows.Add(thead);
            floorTable.Rows.Add(planRow);
            floorTable.Rows.Add(tr);

            // Create Footer
            PopulateTableFooter(cols);
        }

        protected void UnitLinkClicked(object sender, EventArgs e)
        {
            Button btn = (Button)(sender);
            string upId = btn.CommandArgument;

            System.Diagnostics.Debug.WriteLine("LINK Button clicked Of UP ID :" + upId);
        }



As the above code shows, am adding Button to TableCell and have added Click event handler to it. But the click event of button is not clicked and additionally as I click the button the table disappears. On button click I want to show info of that object below the table.

Can you please help me know how to solve this issue on button click event and why does the table disappear on button click ?
Any help is highly appreciated.

Thanks




Answers (2)