2010-11-02 86 views
1

我在ASP.NET中動態創建C#中的表,每行都有一個LinkBut​​ton供用戶刪除記錄。現在的問題是,頁面根本不會進入命令 - 我已經在那裏放置了斷點,它不會進入那裏。這裏是我的代碼:問題動態創建LinkBut​​ton不會進入命令(C#/ ASp.NET)

protected void Page_Init(object sender, EventArgs e) 
     { 
      if (Request.QueryString["id"] != null) 
      { 

       ColorConverter conv = new ColorConverter(); 
       string connection = ConfigurationManager.ConnectionStrings["TPRTestConnectionString"].ConnectionString; 
       TPRDBDataContext dc = new TPRDBDataContext(); 
       DataContext db = new DataContext(connection); 
       Table<SageAccount> SageAccount = db.GetTable<SageAccount>(); 
       Table<InvoiceItem> InvoiceItem = db.GetTable<InvoiceItem>(); 
       Table<Invoice> Invoice = db.GetTable<Invoice>(); 
       Boolean alloweditting = (from s in dc.Invoices where s.id.ToString() == Request.QueryString["id"] select s.alloweditting).Single(); 
       if (alloweditting == false) 
       { 
        dtlsInsert.Visible = false; 
        modalPanel.Visible = false; 
       } 
       int sagepk = (from s in dc.Invoices where s.id.ToString() == Request.QueryString["id"] select s.sageaccount).Single(); 
       lblSageID.Text = (from s in dc.SageAccounts where s.ID == sagepk select s.SageID).Single(); 
       lblDate.Text = DateTime.Now.ToShortDateString(); 


       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 += lnkdel_Command; 

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



           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); 
      } 
      else 
      { 
       Response.Redirect("Invoices.aspx"); 
      } 
     } 

     protected void Page_Load(object sender, EventArgs e) 
     { 



     } 


     protected void lnkdel_Command(object sender, CommandEventArgs e) 
     { 
      string connection = ConfigurationManager.ConnectionStrings["TPRTestConnectionString"].ConnectionString; 


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

你爲什麼要訂閱*兩個*的處理程序'Command'事件? ('lnkdel_Command',然後是匿名委託)。另外,嘗試將'lnkdel_Command'輔助功能修改器更改爲'protected'(不是'private') – RPM1984 2010-11-02 08:52:25

+0

對不起,有點調試代碼仍然存在。正如你所見,我已經改變了我的代碼。我也將它更改爲受保護,但由於某些原因,它仍然沒有進入該方法。 – Chris 2010-11-02 08:58:01

+0

您還應該發佈與LinkBut​​ton相關的asp.net代碼。 – Hal 2010-11-02 09:02:19

回答

0

lnkdel.Command += lnkdel_Command;

應該像

lnkdel.Command += new CommandEventHandler(this.lnkdel); 
+0

它仍然不會進入方法= [ – Chris 2010-11-02 10:02:10

+1

lnkdel.Command + = lnkdel_Command;是有效的,它刪除多餘的代表。 – Win 2010-11-02 20:56:22

相關問題