为表的每一行动态重新创建回发控件(ASP.NET / C#)

时间:2010-11-01 14:10:17

标签: c# asp.net recreate

对于我''table'中的每一行,我都在手动创建,必须有一个LinkBut​​ton来删除与表中的列匹配的数据库行。因为没有2个控件可以具有相同的名称,所以我必须使用GUID来命名它们,因此它们都是唯一的。

问题是,现在当我点击删除时,页面回发没有任何变化,但我被告知我需要重新创建控件 - 但是如果他们的ID是随机生成的,我如何在Page_Load中重新创建它们?这是我的代码:

 Table table = new Table();
            table.GridLines = GridLines.None;
            //table.BorderWidth = 1;
            //table.BorderColor = (System.Drawing.Color)conv.ConvertFromString("black");
            table.Width = Unit.Percentage(100);
            table.GridLines = (GridLines)3;

            TableHeaderRow header = new TableHeaderRow();
            header.BackColor = (System.Drawing.Color)conv.ConvertFromString("#EDEDED");
            foreach (string header2 in new string[] {"", "Quantity", "Rate", "Description", "Nominal Code", "Subtotal" })
            {
                TableCell cell = new TableCell();
                cell.Text = header2;
                header.Cells.Add(cell);
            }

            table.Rows.Add(header);

            var data = (from s in dc.InvoiceItems where s.invoiceid.ToString() == Request.QueryString["id"].ToString() select s);
            foreach (var x in data)
            {

                TableRow row = new TableRow();
                if (x.invoicetext == null)
                {
                    decimal total;
                    try
                    {
                        total = (decimal)x.rate * (decimal)x.quantity;
                    }
                    catch
                    {
                        total = 0;
                    }
                    int i = 0;
                    foreach (string columnData in new string[] {x.id.ToString(), x.quantity.ToString(), x.rate.ToString(), x.description, x.nominalcode, total.ToString("N2") })
                    {
                        TableCell cell = new TableCell();
                        {
                            if (i == 0)
                            {
                                LinkButton lnkdel = new LinkButton();
                                lnkdel.Text = "Delete";
                                lnkdel.ID = "lnkDel" + Guid.NewGuid();

                                 if (alloweditting == false)
                                {
                                    lnkdel.Enabled = false;
                                }
                                 lnkdel.Font.Bold = false;
                                lnkdel.CommandArgument = x.id.ToString();

                                lnkdel.Command += (s, e2) =>
                                {
                                    using (SqlConnection conn = new SqlConnection(connection))
                                    {
                                        SqlCommand comm = new SqlCommand("DELETE FROM InvoiceItem WHERE id = @id", conn);
                                        comm.Parameters.AddWithValue("@id", e2.CommandArgument);
                                        conn.Open();
                                        try
                                        {
                                            comm.ExecuteNonQuery();
                                        }
                                        catch (Exception ex)
                                        {
                                            Response.Write(ex);
                                        }
                                    }
                                };

                                cell.Controls.Add(lnkdel);
                                i++;
                            }
                            else
                            {
                                cell.Text = columnData;
                            }


                        }

                        row.Cells.Add(cell);
                    }



                    runningtotal = runningtotal + total;

                }
                else
                {
                    int i = 0;

                    foreach (string columnData in new string[] {x.id.ToString(), x.invoicetext })
                    {
                        TableCell cell = new TableCell();

                            if (i == 0)
                            {
                                LinkButton lnkdel = new LinkButton();
                                lnkdel.Text = "Delete";
                                lnkdel.ID = "lnkDel" + Guid.NewGuid();

                                 if (alloweditting == false)
                                {
                                    lnkdel.Enabled = false;
                                }
                                 lnkdel.Font.Bold = false;
                                lnkdel.CommandArgument = x.id.ToString();
                                rowid = lnkdel.CommandArgument;
                                lnkdel.Command += (s, e2) =>
                                {
                                    using (SqlConnection conn = new SqlConnection(connection))
                                    {
                                        SqlCommand comm = new SqlCommand("DELETE FROM InvoiceItem WHERE id = @id", conn);
                                        comm.Parameters.AddWithValue("@id", rowid);
                                        conn.Open();
                                        try
                                        {
                                            comm.ExecuteNonQuery();
                                        }
                                        catch (Exception ex)
                                        {
                                            Response.Write(ex);
                                        }
                                    }
                                };

                                cell.Controls.Add(lnkdel);
                                i++;
                            }
                            else
                            {
                                cell.Text = columnData;
                                cell.ColumnSpan = 5;
                            }
                            row.Cells.Add(cell);

                        }

                }

                switch (x.formatoptions)
                {
                    case 1:
                        row.ForeColor = (System.Drawing.Color)conv.ConvertFromString("black");
                        row.Font.Bold = false;
                        break;
                    case 2:
                        row.ForeColor = (System.Drawing.Color)conv.ConvertFromString("black");
                        row.Font.Bold = true;
                        break;
                    case 3:
                        row.ForeColor = (System.Drawing.Color)conv.ConvertFromString("red");
                        row.Font.Bold = false;
                        break;
                    case 4:
                        row.ForeColor = (System.Drawing.Color)conv.ConvertFromString("red");
                        row.Font.Bold = true;
                        break;
                }
                table.Rows.Add(row);
            }

            TableFooterRow row2 = new TableFooterRow();
            TableCell cell2 = new TableCell();
            cell2.Text = "<span style\"text-align: right; width: 100%;\">Total = <b>" + runningtotal.ToString("N2") + "</b></span>";
            cell2.ColumnSpan = 6;
            row2.Cells.Add(cell2);
            table.Rows.Add(row2);

            var update = (from s in dc.Invoices where s.id.ToString() == Request.QueryString["id"] select s).Single();
            update.total = runningtotal;

            dc.SubmitChanges();
            datatable.Controls.Clear();
            datatable.Controls.Add(table);
        }

2 个答案:

答案 0 :(得分:0)

不要使用GUID。从rowid中获取ID值,因此它是一个可重复的值,如“row_784”。

例如,而不是

lnkdel.ID = "lnkDel" + Guid.NewGuid();

使用

lnkdel.ID = "lnkDel" + x.id.ToString();

答案 1 :(得分:0)

您需要在Page_Init中创建它们,而不是Page_Load。您只需要将调用创建表的代码移到Page_Init,一切都应该按预期工作。