I'm using a gridview and it is bound to a datatable so I've autogeneratecolumns set to true. Next I added a linkbutton dynamically to the last cell of the row and i see the linkbutton appearing on the gridview. Now I'm not able to associate any event with the button. I'd like to know if events get triggered when using dynamically added linkbutton on a gridview with autogeneratecolumns set to true. Here is what I've in my code
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";
}
}
and 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>
I also do not understand clearly the problem you have, but isn`t something about the old live() function that could handle the dynamically generated elements. I think .delegate() can handle this.