单击行时OnRowCommand =“grd_RowCommand”事件未触发

时间:2013-08-15 10:46:00

标签: c# gridview rowcommand

这里我使用代码隐藏动态绑定网格列,因为我的GetSocailAnalytics方法根据传递的参数返回动态列。使用数据表绑定网格列后,当我点击网格行时,我的grd_OnRowCommand事件未触发。网格成功绑定。任何人都可以帮我解决这个问题。我的代码还有......

<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 个答案:

答案 0 :(得分:0)

  

单击按钮时会引发RowCommand事件   GridView控件。

单击网格行

时不会触发

解决方案是使用CommandName ="Filter"

将按钮(LinkBut​​ton)列添加到gridview