OnRowCommand="grd_RowCommand" event not firing when click on row

424 views Asked by At

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

There are 1 answers

0
Damith On

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"