表单元格中的按钮未触发事件asp.net c#

时间:2018-11-28 18:21:12

标签: c# asp.net events button onclick

没有找到关于此的具体答案,所以它就来了...我正在foreach循环中创建HTML表,在该循环中,每次为其对应顺序创建行。我有两个按钮,如下面的代码所示:

 private void CreateOrderTable(List<Order> orders)
    {
        OrderModel model = new OrderModel();

        foreach (Order order in orders)
        {
            Order orderN = new Order();



            //Create HTML Table
            Table table = new Table { CssClass = "cartTable" };
            TableRow a = new TableRow();
            TableRow b = new TableRow();

            //Crear cells for row a
            TableCell a1 = new TableCell { RowSpan = 2, Width = 50 };
            TableCell a2 = new TableCell
            {
                Text = string.Format(order.OrderID.ToString()),
                HorizontalAlign = HorizontalAlign.Left,
                Width = 350
            };
            TableCell a3 = new TableCell { Text = "Monto:<hr/>" };
            TableCell a4 = new TableCell { Text = "Restaurante:<hr/>" };
            TableCell a5 = new TableCell { Text = "Estado<hr/>" };
            TableCell a6 = new TableCell { Text = "Tarjeta<hr/>" };
            TableCell a7 = new TableCell { Text = "<asp:LinkButton  OnClick=\"AcceptPayment\" runat=\"server\" class=\"myButtonGreen\">Aceptar Pago</asp:LinkButton>" };
            TableCell a8 = new TableCell { Text = "<asp:LinkButton  OnClick=\"RejectOrder\" runat=\"server\" class=\"myButtonRed\">Anular Orden</asp:LinkButton>" };

            //Create cells for row b
            TableCell b1 = new TableCell { };
            TableCell b2 = new TableCell { Text = order.Amount.ToString() };
            TableCell b3 = new TableCell { Text = order.Restaurante.ToString() };
            TableCell b4 = new TableCell { Text = order.OrderState.ToString() };
            TableCell b5 = new TableCell { Text = order.CardNumber + " " + order.CardDate };
            TableCell b6 = new TableCell { };


            //Add cells to columns
            a.Cells.Add(a1);
            a.Cells.Add(a2);
            a.Cells.Add(a3);
            a.Cells.Add(a4);
            a.Cells.Add(a5);
            a.Cells.Add(a6);
            a.Cells.Add(a7);
            a.Cells.Add(a8);




            b.Cells.Add(b1);
            b.Cells.Add(b2);
            b.Cells.Add(b3);
            b.Cells.Add(b4);
            b.Cells.Add(b5);
            b.Cells.Add(b6);

            //Add rows to table
            table.Rows.Add(a);
            table.Rows.Add(b);

            //Add table to panel
            pnlShoppingCart.Controls.Add(table);

        }
    }

    protected void AcceptPayment(object sender, EventArgs e)
    {
    }

    protected void RejectOrder(object sender, EventArgs e)
    {

    }

和我调用的OnClick事件没有被访问,因为我在VS上放置了调试标记。

为什么会这样?

此外,我如何将int传递给事件?

1 个答案:

答案 0 :(得分:1)

您可能应该创建实际的LinkButton(或LinkButton)对象,而不是创建HtmlAnchor的字符串,然后将它们添加到表单元格的Controls集合中。

>

赞:

var lb = new LinkButton();
lb.Text = "Aceptar Pago";
lb.CssClass = "myButtonGreen";
lb.Click += AcceptPayment;

a7.Controls.Add(lb);