2014-01-24 162 views
0

我正在使用gridview並將其綁定到數據表,因此我將autogeneratecolumns設置爲true。接下來我動態地添加了一個linkbutton到行的最後一個單元格,並且我看到了出現在gridview上的linkbutton。現在我無法將任何事件與按鈕相關聯。我想知道在使用autogeneratecolumns設置爲true的gridview上使用動態添加的linkbutton時是否觸發事件。以下是我在我的代碼已經使動態創建的鏈接按鈕點擊觸發事件

protected void btnSearch_Click(object sender, EventArgs e) 
{ 

Datatable retval = // api call to a method. returns valida datatable 

    if (retval != null) 
    { 
     if (retval.Rows.Count > 0) 
     { 
      GridViewSearchResult.Visible = true; 
      GridViewSearchResult.DataSource = retval; 
      GridViewSearchResult.DataBind(); 
    } 
    } 
} 

protected void GridViewSearchResult_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 

    LinkButton lb; 
    int j = e.Row.Cells.Count; 
    string HeaderText; 

    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 

     lb = new LinkButton(); 
     lb.Text = "edit"; 
     //lb.CommandArgument = "edit"; 
     //lb.CommandName = "edit"; 
     //lb.Command += LinkButton_Command; 
     //lb.Click += new EventHandler(onLinkClick); 
     //ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(lb); 
     //e.Row.Cells[j-1].Controls.Add(l); 
     e.Row.Cells[j-1].Controls.Add(lb); 

     lb.Click += new EventHandler(lbtn_Click); 

    } 

} 


protected void GridViewSearchResult_RowCommand(object sender, CommandEventArgs e) 
{ 
    switch (e.CommandName.ToLower()) 
    { 
     case "edit": 
      Server.Execute("VerifyContact.aspx"); 
      break; 
     default: 
      break; 
    } 
} 
protected void lbtn_Click(object sender, EventArgs e) 
{ 
    //if (e. == "delete") 
    { 
     Server.Execute("VerifyContact.aspx"); 
    } 
} 
protected void LinkButton_Command(object sender, CommandEventArgs e) 
{ 
    if (e.CommandName == "edit") 
    { 
     LinkButton lb = (LinkButton)sender; 
     lb.Text = "OK"; 
    } 
} 

和ASP

<div id="searchResultGrid"> 
     <asp:GridView ID="GridViewSearchResult" runat="server" 
      OnRowDataBound="GridViewSearchResult_RowDataBound" 
      onselectedindexchanged="GridViewSearchResult_SelectedIndexChanged" 
      onRowEditing = "GridViewSearchResult_RowEditing" 
      onRowCommand = "GridViewSearchResult_RowCommand"> 

     </asp:GridView> 
    </div> 
+1

在此處發佈您的代碼 –

回答

0

我也不太清楚的瞭解關於老活()函數,你有這個問題,但isn`t東西可以處理動態生成的元素。我認爲.delegate()可以處理這個。