我正在使用gridview并且它绑定到数据表,因此我将autogeneratecolumns设置为true。接下来,我动态地向行的最后一个单元格添加了一个linkbutton,我看到gridview上出现了linkbutton。现在我无法将任何事件与按钮相关联。我想知道在自动生成列设置为true的gridview上使用动态添加的链接按钮时是否触发事件。这是我在我的代码中的内容
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>
答案 0 :(得分:0)
我也不清楚你所遇到的问题,但对于可以处理动态生成元素的旧live()函数并不了解。我认为.delegate()可以解决这个问题。