动态创建的按钮不会在C#中触发事件

时间:2017-08-03 15:28:31

标签: c# asp.net

我在动态创建的表中动态创建按钮。由于某种原因,按钮事件不会触发。我想知道是否必须在<div id="logo-container"> <div id="coopLogo"> <div>Brand 1 logo(longer)</div> <div>+</div> <div>B2 logo</div> </div> </div> <br> <div id="middle-indicator"></div>上添加事件,因为我需要会话数据来创建表格和按钮。我已经尝试从登录页面获取客户ID并将其放入URL并使用它来执行create table方法,但它不起作用。

init

编辑:这是goButton_Click()和viewButton_OnClick()方法。

for (int i = populate.getBeginCount(); i < populate.getEndCount(); i++)
            {
                TableRow tr = new TableRow();
                for (int j = 0; j < 10; j++)
                {
                    DataRow dr1 = QueryHandler.GetDataRow("SELECT * FROM [order_line] WHERE order_id=" + Convert.ToInt32(dt.Rows[i].Field<long>(1)) + ";", CommandType.Text);
                    TableCell tc = new TableCell();
                    switch (j)
                    {
                        case 0:
                            HyperLink Order = new HyperLink();
                            Order.Text = dt.Rows[i].Field<long>(1).ToString();
                            Order.NavigateUrl = "/customer-portal/orderdetails.aspx?partnerid=" + dt.Rows[i].Field<string>(18);
                            tc.Controls.Add(Order);
                            tc.ToolTip = dt.Rows[i].Field<long>(1).ToString();
                            break;
                        case 1:
                            tc.Text = dt.Rows[i].Field<string>(21);
                            break;
                        case 2:
                            product pro = new product();
                            if (pro.GetProduct(dr1.Field<int>(3), customerId))
                                tc.Text = pro.product_type;
                            else
                                tc.Text = "empty";
                            break;
                        case 3:
                            product prod = new product();
                            if (prod.GetProduct(dr1.Field<int>(3), customerId))
                                tc.Text = prod.company;
                            else
                                tc.Text = "empty";
                            break;
                        case 4:
                            tc.Text = dt.Rows[i].Field<string>(3);
                            break;
                        case 5:
                            DropDownList drop = new DropDownList();
                            ListItem act = new ListItem();
                            act.Text = "ACTION";
                            ListItem Reissue = new ListItem();
                            Reissue.Text = "Reissue";
                            ListItem cancel = new ListItem();
                            cancel.Text = "Cancel";
                            drop.Items.Add(act);
                            drop.Items.Add(Reissue);
                            if (dt.Rows[i].Field<string>(3) == "PENDING")
                                drop.Items.Add(cancel);
                            Button go = new Button();
                            go.Text = "Go";
                            go.ID = "ButtonGo" + i;
                            go.Click += new System.EventHandler(goButton_Click);                           
                            go.CssClass = "btn btn-sm btn-default filter-cancel";
                            go.DataBind();
                            tc.Controls.Add(drop);
                            tc.Controls.Add(go);
                            break;
                        case 6:
                            tc.Text = dt.Rows[i].Field<DateTime?>(16).ToString();
                            break;
                        case 7:
                            tc.Text = dt.Rows[i].Field<DateTime?>(17).ToString();
                            break;
                        case 8:
                            Button b = new Button();
                            b.Text = "View";
                            b.Click += new System.EventHandler(viewButton_OnClick);
                            b.ID = "Button" + i;
                            b.CssClass = "btn btn-sm btn-default filter-cancel";
                            tc.Controls.Add(b);
                            break;
                        case 9:
                            Button c = new Button();
                            c.Text = "Print Receipt";
                            c.Click += new System.EventHandler(printButton_OnClick);
                            c.ID = "PrintButton" + i;
                            c.CssClass = "btn btn-sm btn-default filter-cancel";
                            tc.Controls.Add(c);
                            break;
                    }
                    if (tc.Text != "")
                        tr.Cells.Add(tc);
                    else if (tc.Text == "" && j == 6)
                    {
                        tc.Text = "Not Completed";
                        tr.Cells.Add(tc);
                    }
                    else if (tc.Text == "" && j == 7)
                    {
                        tc.Text = "No Expiration Date";
                        tr.Cells.Add(tc);
                    }
                    else if (tc.Controls.Count > 0)
                        tr.Cells.Add(tc);
                }
                Tableorders.Rows.Add(tr);
            }

0 个答案:

没有答案