RadGrid AddNewRecordButton problem on Prerender event

3 posts, 1 answers
  1. Gaurav
    Gaurav avatar
    7 posts
    Member since:
    Nov 2011

    Posted 27 Jun 2012 Link to this post

    Hi,
    I am using RadGrid from telerik version 2012.1.215.40.

    I have two methods RadGrid_NeedDataSource and Page_Prerender event

    I am trying to hide AddNewrecordButton based on user permissions

    protected override void OnPreRender(EventArgs e)
           {
               try
               {
                 
                   //Hide controls on the page based on permission
                  //Code to hide control
                  // code to hide RadGrid AddNewRecordButton
                    RadGrid1.MasterTableView.CommandItemSettings.ShowAddNewRecordButton = false;                  
                }
                   base.OnPreRender(e);
               }
               catch (Exception ex)
               {
                   HandleException(ex, ErrorControl);
               }
           }

    This code doesn't hide the AddNewRecordButton from the grid.
    But if i write the same code in RadGrid_NeedsdataSource event, it works. I also tried radGrid_PreRender event but that also not working.
    If i add RadGrid.Rebind() method in prerender event, it works. But it fires the NeedsdataSource event again that i don't want.
    All my code to hide/show controls based on the permissions is inside the PreRender event. So i don't want to use NeedsDataSource event just for hide/show of Add new record button.
    Please suggest what is wrong with the PreRender event.
  2. Answer
    Tsvetina
    Admin
    Tsvetina avatar
    2481 posts

    Posted 02 Jul 2012 Link to this post

    Hi Gaurav,

    It seems like the PreRender event comes too late in the grid lifecycle and that is why the property value change is not reflected. However, you can manually hide the buttons in PreRender with the following code:
    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem).Length > 0)
        {
            GridCommandItem commandItem = RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0] as GridCommandItem;
            commandItem.FindControl("AddNewRecordButton").Visible = false;
            commandItem.FindControl("InitInsertButton").Visible = false;
        }
    }

    (the AddNewRecordButton actually consists of two button controls and I am hiding both)

    Note that in case the command item is shown at the top and bottom of the grid, you need to also access the command item at index [1] to hide its buttons.

    Kind regards,
    Tsvetina
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. Gaurav
    Gaurav avatar
    7 posts
    Member since:
    Nov 2011

    Posted 03 Jul 2012 Link to this post

    Thank you very much. The posted solution is working fine in both Page_PreRender event and Radgrid_PreRender event.
Back to Top