OnRowCommand=“grd_RowCommand” event not firing when click on row

那年仲夏 提交于 2019-12-14 03:18:40

问题


here i bind grid columns dynamically using code behind because my GetSocailAnalytics method return dynamic column according to parameter passed.after binding column of grid using data table my grd_OnRowCommand event not firing when i click on grid row. grid is successfully bind. can any one help me out this issue.here is my code...

<asp:GridView ID="grd" EnableViewState="true" AutoGenerateColumns="false" OnRowCommand="grd_RowCommand"
                    runat="server" OnRowDataBound="grd_RowDataBound">
                    <Columns>
                    </Columns>
                </asp:GridView>
private void GetData()
{
    try
    {
        int TotalRecords = 0;
        DataTable dt = ClsSocialManager.GetSocialAnalytics(Convert.ToInt32(hdnReferrerId.Value), Convert.ToInt32(hdnReferralId.Value), out TotalRecords, Convert.ToInt32(hdnPageIndex.Value));
        if (dt != null && dt.Rows.Count > 0)
        {
            BindTemplateFiled(dt);
            grd.Visible = true;

        }
        else
        {
            grd.Visible = false;
        }
        lblStatus.Text = TotalRecords.ToString() + " Record(s) found";
    }
    catch (Exception ex)
    {
        lblStatus.Text = "Some Error Occured " + ex.Message;
        lblStatus.CssClass = "ErrMsg";
    }
}

//Start Crearting GridColumn Dynamically
class LinkColumn : ITemplate
{
    public void InstantiateIn(System.Web.UI.Control container)
    {
        LinkButton link = new LinkButton();
        link.ID = "lnkbtnReferrerHost";
        link.DataBinding += new EventHandler(this.link_DataBinding);
        link.CommandName = "sad";
        container.Controls.Add(link);
    }

    private void link_DataBinding(Object sender, EventArgs e)
    {
        LinkButton lnkReferrerHost = (LinkButton)sender;
        GridViewRow row = (GridViewRow)lnkReferrerHost.NamingContainer;
        lnkReferrerHost.Text = Convert.ToString((((System.Data.DataRowView)(row.DataItem))).Row[1]);
        lnkReferrerHost.CommandArgument = Convert.ToString((((System.Data.DataRowView)(row.DataItem))).Row[0]);
        //lnkReferrerHost.CommandName = "Filter";
    }
}   

private void BindTemplateFiled(DataTable dt)
{
    for (int i = 0; i < dt.Columns.Count; i++)
    {
        if (dt.Columns[i].ColumnName == "Referrer Host")
        {
            var lnkbtnReferrerHost = new TemplateField();
            lnkbtnReferrerHost.ItemTemplate = new LinkColumn();
            lnkbtnReferrerHost.HeaderText = dt.Columns[i].ColumnName;                
            grd.Columns.Add(lnkbtnReferrerHost);
        }
        else
        {
            BoundField field = new BoundField();
            field.DataField = dt.Columns[i].ColumnName;
            field.HeaderText = dt.Columns[i].ColumnName;
            grd.Columns.Add(field);
        }

    }
    grd.DataSource = dt;
    grd.DataBind();
}
//End Crearting GridColumn Dynamically


protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {
        if (e.CommandName == "Filter")
        {
            GridViewRow gvr = (GridViewRow)grd.Rows[((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex];
            LinkButton lbtn = (LinkButton)gvr.FindControl("lnkReferrerHost");
            hdnReferrerId.Value = Convert.ToString(Convert.ToInt32(e.CommandArgument));
            lblCurrentPage.Text = lbtn.Text;
            GetData();
        }
    }
    catch
    {
    }
}

回答1:


The RowCommand event is raised when a button is clicked in the GridView control.

it is not not firing when you click on grid row

solution is adding button (LinkButton) column to the gridview with CommandName ="Filter"



来源:https://stackoverflow.com/questions/18251115/onrowcommand-grd-rowcommand-event-not-firing-when-click-on-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!