0
Answer

Button Click in a TableCell never fires and table disappears

Terry

Terry

9y
843
1

In my ASP.NET Webforms app, I have a `table` in which I add all data dynamically. One row contains `Buttons` in each cell. I want the button to fire `onclick` event when the user clicks on it. But, with the below code, the event never fires and the table disappears. Here's the code :

        <asp:Table ID="floorTable" runat="server"  Width="100%" GridLines="Both">
        </asp:Table>

In Code behind :

       // This method is called on a DropDownList SelectedItemChanged Event - so
       // the buttons cannot be created in Page_Load or so. Have to create 
       // totally based on the DropDown selected item.
        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;

            foreach (UnitPattern up in fp.UnitPattern)
            {                
                tc = new TableCell();

                Button imgBtn = new Button();
                                
                // On Adding BELOW Line - ERROR - 0x800a1391 - JavaScript runtime error: 'UnitLinkClicked' is undefined
                //imgBtn.Attributes.Add("onClick", "UnitLinkClicked(this)");

                imgBtn.CommandArgument = up.UnitPatternId.ToString(); // I want to know which button is pressed. So, sort of Tag
                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;
                }

                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);
        }

In CODE OF SELECTEDiNDEXCHANGED ADDED
My DropDown_SelectedIndexChanged Code :
        protected void floorDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (selectedProject == null)
                selectedProject = _db.Projects.Find(projectsList.SelectedValue);

            System.Diagnostics.Debug.WriteLine("SELCTED Project = " + selectedProject.ProjectId);

            // "Select Floor" is selected, so Hide floor Table
            if (floorDropDownList.SelectedValue == "-1")
            {
                floorTable.Visible = false;
            }
            else
            {
                int floorNo = int.Parse(floorDropDownList.SelectedValue);
                if (floorNo > 0)
                {
                    PopulateFloorRow(floorNo, (FloorPattern)selectedProject.FloorPattern.ElementAt(floorNo - 1));
                }
            }
        }


If If I had selected "3" in my drop down, the table appears as expected. I click on a button and the table disappears, but the value in the drop down in still "3" only.

With the above code, I when I click on a button, the `UnitLinkClicked` event is never fired (I had added breakpoint) and the whole table disappears.

Can you say what problem can this be ? A button by default is meant to be `AutoPostBack` & it doesn't have that property too. What am I missing here and how to solve this. Am stuck on this since days and trying to figure out.

Any help is highly appreciated.

Thanks.