2014-07-10 68 views
0

我有以下代碼,其中我通過數據庫值填充數據表3列。並添加另外1個名爲ACTION的列,其中已經動態生成了LINKBUTTON。
但是當我點擊這個按鈕它的事件不會觸發。每3秒動態鏈接按鈕單擊事件不起作用

private void fillConfGrid() 
{ 
    try 
    { 
     DataTable table = new DataTable(); 
     DataBase m_Connection = new DataBase(); 
     m_Connection.OpenDB("Database"); 
     string strTemp = "select col1,col2,col3 from Table1"; 
     OdbcCommand cmdSelect = new OdbcCommand(strTemp, m_Connection.oCon); 
     OdbcDataAdapter da = new OdbcDataAdapter(cmdSelect); 
     DataSet ds = new DataSet(); 
     da.Fill(ds); 
     table = ds.Tables[0]; 
     table.Columns.Add("Action", typeof(string)); 

     confGrid.DataSource = table; 
     confGrid.DataBind(); 
     //LinkButton lbl=null; 
     for (int i = 0; i < confGrid.Rows.Count; i++) 
     { 
      LinkButton lbl = new LinkButton(); 
      lbl.ID = confGrid.Rows[i].Cells[0].Text; 
      lbl.CommandArgument = confGrid.Rows[i].Cells[0].Text.Trim() + "," + confGrid.Rows[i].Cells[1].Text.Trim(); 
      lbl.Text = "Disconnect"; 
      lbl.Click += new EventHandler(lbl_Click); 
      lblConfError.Text = confGrid.Rows[i].Cells[0].Text.Trim() + "," + confGrid.Rows[i].Cells[1].Text.Trim(); 
      confGrid.Rows[i].Cells[3].Controls.Add(lbl);     
     } 

     m_Connection.CloseDB(); 
    } 
    catch (Exception ex) 
    { 
     Response.Write("<script>alert('error 1: " + ex.Message.ToString() + "');</script>"); 
    } 
} 


protected void lbl_Click(object sender, EventArgs e) 
{   
    lblConfError.Text = "Click event"; 
    CtiWS CtiWS1 = new CtiWS(); 
    LinkButton button = (LinkButton)sender; 
    GridViewRow row = (GridViewRow)button.NamingContainer; 
    if (row != null) 
    { 
     lblConfError.Text = "Click event"; 
     string cmdArgs = ((LinkButton)sender).CommandArgument.ToString(); 
     Response.Write("<script>alert('Argument : " + cmdArgs + "');</script>"); 
     string[] confParts = cmdArgs.Split(','); 
     lblConfError.Text = confParts[0] + " : " + confParts[1]; 
     CtiWS1.SendCode("", "", "K:" + confParts[0] + ":" + confParts[1], HttpContext.Current.Session["HOSTID"].ToString()); 
    }   
} 

回答

-1

你這裏的問題是,按鈕被動態生成的,等後返回之後

我打電話fillConfGrid()函數,不存在。您的選項有:

  1. 重新創建OnInit方法中的頁面。
  2. 嘗試使用Form集合處理onClick事件。
+0

這非常有幫助。它解決了我的問題。謝謝@ DNewman62 – user3713775