2014-01-24 131 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()可以处理这个。