GridButtonColumn does not fire in programatically created RadGrid

2 posts, 0 answers
  1. Richard
    Richard avatar
    26 posts
    Member since:
    Sep 2011

    Posted 23 Nov 2018 Link to this post

    I have a need to programmatically create a RadGrid based on the number of elements from another table.  So I have the following:

    foreach (DataRow customerRow in dtDistinctMonitors.Rows)
                {
                    //Get Data
                    DataRow[] rowData = dtMonitors.Select("tenantID = '" + tenantID + "'");

                    //Add Label
                    Label lblName = new Label();
                    lblName.Text = "Channel: xxxx";
                    lblName.Font.Size = FontUnit.Large;
                    PlaceHolder1.Controls.Add(lblName);

                    //Add RadGrid
                    RadGrid RadGrid1 = new RadGrid();
                    RadGrid1.ID = customerRow["channelID"].ToString();

                    //Add RadGrid to the Controls collection of the placeholder
                    PlaceHolder1.Controls.Add(RadGrid1);

                    RadGrid1.DataSource = rowData;
                    RadGrid1.MasterTableView.DataKeyNames = new string[] { "monitorID" };
                    RadGrid1.AllowPaging = false;
                    RadGrid1.MasterTableView.AutoGenerateColumns = false;
                    RadGrid1.Skin = "Silk";
                    RadGrid1.RenderMode = RenderMode.Lightweight;
                    RadGrid1.Width = Unit.Percentage(100);
                    GridBoundColumn boundColumn;
                    boundColumn = new GridBoundColumn();
                    RadGrid1.MasterTableView.Columns.Add(boundColumn);
                    boundColumn.DataField = "monitorName";
                    boundColumn.HeaderText = "Monitor Name";
                    GridButtonColumn buttonColumn;
                    buttonColumn = new GridButtonColumn();
                    buttonColumn.Text = "Delete";
                    buttonColumn.ButtonType = GridButtonColumnType.LinkButton;
                    buttonColumn.HeaderStyle.Width = Unit.Pixel(100);
                    buttonColumn.CommandArgument = "monitorID";
                    buttonColumn.CommandName = "lnkDelete_Command";
                    RadGrid1.MasterTableView.Columns.Add(buttonColumn);
                }

    And below I have lnkDelete_Command as:

    public void lnkDelete_Command(object sender, CommandEventArgs e)
        {
            using (SqlConnection sqlConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["stringvalue"].ConnectionString))
            {
                string ID = e.CommandArgument.ToString();

                //DELETE FROM WHERE ID = ID
            }
        }

     

    Yet, when I click on the LinkButton created it doesn't fire.  It doesn't really do anything at all but creates a second instance of the button.  Am I missing something simple here?

  2. Marin Bratanov
    Admin
    Marin Bratanov avatar
    4650 posts

    Posted 26 Nov 2018 Link to this post

    Hello Richard,

    If you will be creating the grid programmatically, I advise that you review the following articles. The key points to look for in them are:

    • which events you must create the grid in (generally, using Page_Init is the best option, and Page_Load is the latest option)
    • ensure the grid is re-created after each postback
    • the order of setting the column properties and adding the columns to the grid
    • how to add event handlers dynamically (usually handing the ItemCommand event will be easiest as it is likely you will be using it for other tasks and not just the custom command)
    • that you should add the grid to the page at the end of its settings creation

    That said, here are the links:

     


    Regards,
    Marin Bratanov
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top