This is a migrated thread and some comments may be shown as answers.

RadGrid AddNewRecordButton problem on Prerender event

2 Answers 166 Views
Ajax
This is a migrated thread and some comments may be shown as answers.
Gaurav
Top achievements
Rank 1
Gaurav asked on 27 Jun 2012, 09:57 AM
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 Answers, 1 is accepted

Sort by
0
Accepted
Tsvetina
Telerik team
answered on 02 Jul 2012, 08:10 AM
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.
0
Gaurav
Top achievements
Rank 1
answered on 03 Jul 2012, 07:10 AM
Thank you very much. The posted solution is working fine in both Page_PreRender event and Radgrid_PreRender event.
Tags
Ajax
Asked by
Gaurav
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Gaurav
Top achievements
Rank 1
Share this question
or