0

我正在使用gridview,它绑定到一个数据表,所以我将autogeneratecolumns 设置为true。接下来,我将一个链接按钮动态添加到该行的最后一个单元格,我看到该链接按钮出现在 gridview 上。现在我无法将任何事件与按钮相关联。我想知道在自动生成列设置为 true 的网格视图上使用动态添加的链接按钮时是否触发了事件。这是我的代码中的内容

    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>
4

1 回答 1

0

我也不清楚您遇到的问题,但与可以处理动态生成的元素的旧 live() 函数无关。我认为 .delegate() 可以处理这个问题。

于 2014-01-24T13:15:15.763 回答