locked
Grid View 'OnRowCommand' event is not firing RRS feed

  • Question

  • Button is created to GridView from code behind using below code and when i click the button on GridView , i can't see grid view and 'OnRowCommand' event is not fired.

    aspx code:

    <asp:GridView ID="grdRoles" runat="server" AllowCustomPaging="True" AllowPaging="True" OnRowCommand="grdRoles_RowCommand" HorizontalAlign="Center" OnRowDataBound="grdRoles_RowDataBound">
                          
                            <HeaderStyle BackColor="#CC0000" Font-Bold="True" ForeColor="White" />
                        </asp:GridView>

    Code Behind:

    DataTable dt = new DataTable();
                                    dt.Columns.Add("Roles");
                                    dt.Columns.Add("View");
                                    foreach (Role r in d.Roles)
                                    {

                                        foreach (RoleMember m in r.Members)
                                        {
                                            if (string.Compare(m.Name, Resource.Domain + UserId, StringComparison.InvariantCultureIgnoreCase) == 0)
                                            {
                                                DataRow dr = dt.NewRow();
                                                dr["Roles"] = r.Name;                                       
                                                dt.Rows.Add(dr);

    grdRoles.RowCommand += new GridViewCommandEventHandler(grdRoles_RowCommand);                                            grdRoles.DataSource = dt;
                                               
                                                grdRoles.DataBind();
                                                
                                            }
                                        }
                                    }

     protected void grdRoles_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.DataItem!=null)
                {
                    // CREATE A Button AND IT TO EACH ROW.
                    Button btnView = new Button();
                    btnView.ID = "btnView";
                    btnView.Text = "View";
                    btnView.CommandName = "Click";
                    btnView.CommandArgument = e.Row.Cells[0].Text;
                    e.Row.Cells[1].Controls.Add(btnView);
                }
            }

            protected void grdRoles_RowCommand(object sender, GridViewCommandEventArgs e)
            {
                if(e.CommandName=="Click")
                {
                    // action
                }
            }

    • Moved by CoolDadTx Friday, January 26, 2018 2:51 PM ASP.NET related
    Friday, January 26, 2018 7:53 AM

All replies

  • Please post questions related to ASP.NET in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, January 26, 2018 2:51 PM