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

Change edit mode on load

5 Answers 114 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Eric Klein
Top achievements
Rank 1
Eric Klein asked on 31 Dec 2009, 08:53 PM
I have a screen with a few grids on it.  Here is a copy of one of those grid
<telerik:RadPageView ID="pvRegistration" runat="server">  
                                    <telerik:RadAjaxPanel ID="RadAjaxPanel2" runat="server">  
                                        <telerik:RadGrid ID="rgRegistration" runat="server" GridLines="None" AllowSorting="True" 
                                            AllowPaging="true" PageSize="10" AllowMultiRowEdit="false" AutoGenerateColumns="False" 
                                            ShowStatusBar="true" OnNeedDataSource="Registration_NeedDataSource" OnItemDataBound="Registration_ItemDataBound" 
                                            OnUpdateCommand="RegistrationGrid_UpdateCommand" OnInsertCommand="RegistrationGrid_InsertCommand" 
                                            OnDeleteCommand="RegistrationGrid_DeleteCommand"   
                                            Skin="Office2007" Width="100%" > 
                                            <MasterTableView CommandItemDisplay="Top" DataKeyNames="RepFirmID" EditMode="InPlace">  
                                                <Columns> 
                                                    <telerik:GridEditCommandColumn UniqueName="EditCommandColumn">  
                                                        <HeaderStyle Width="50px" /> 
                                                    </telerik:GridEditCommandColumn> 
                                                    <telerik:GridTemplateColumn HeaderText="Firm Type" SortExpression="FirmTypeDescription" 
                                                        UniqueName="FirmType">  
                                                        <ItemTemplate> 
                                                            <asp:Label ID="lbFirmType" runat="server" Text='<%# Eval("FirmTypeDescription")%>' /> 
                                                        </ItemTemplate> 
                                                        <EditItemTemplate> 
                                                            <telerik:RadComboBox ID="rcbFirmType" DataTextField="FirmTypeDescription" DataValueField="FirmTypeID" 
                                                                runat="server" Width="300px" /> 
                                                            <asp:RequiredFieldValidator ControlToValidate="rcbFirmType" ErrorMessage="*" InitialValue="Select Type" 
                                                                runat="server" ToolTip="Type is  Required" FontColor="Red" /> 
                                                            <asp:Label runat="server" ID="lbFirmTypeID" Visible="false" Text='<%# Eval("FirmTypeID") %>'></asp:Label></EditItemTemplate>  
                                                    </telerik:GridTemplateColumn> 
                                                    <telerik:GridButtonColumn ConfirmText="Remove this Firm?" ConfirmDialogType="RadWindow" 
                                                        CommandName="Delete" ConfirmTitle="Delete" Text="Delete" ShowInEditForm="false">  
                                                        <HeaderStyle Width="25px" /> 
                                                    </telerik:GridButtonColumn> 
                                                </Columns> 
                                                <EditFormSettings> 
                                                    <EditColumn ButtonType="ImageButton" InsertText="Insert" UpdateText="Update" UniqueName="EditCommandColumn1" 
                                                        CancelText="Cancel">  
                                                    </EditColumn> 
                                                    <FormTableButtonRowStyle HorizontalAlign="Right"></FormTableButtonRowStyle> 
                                                </EditFormSettings> 
                                                <PagerStyle Mode="NumericPages"></PagerStyle> 
                                            </MasterTableView> 
                                        </telerik:RadGrid> 
                                        <button onclick="ShowForm('FirmType'); return false;">  
                                            Edit Type</button> 
                                    </telerik:RadAjaxPanel> 
                                </telerik:RadPageView> 

at run time I need to change the way the grid works according to Page.User.IsInRole

if the user is not in the correct role I want to display the grid but reomve the edit, delete, and add ne functionality.

I tried
  protected void Registration_PreRender(object sender, EventArgs e)  
        {  
            if (!Page.User.IsInRole("COMPLIANCE USER"))  
            {  
                  
                rgRegistration.AllowAutomaticInserts = false;  
                rgRegistration.AllowAutomaticUpdates = false;  
                rgRegistration.AllowAutomaticDeletes = false;  
                //rgRegistration.MasterTableView.RenderColumns[3].Display = false;  
            }  
        } 
but the grid still had the functionality.  is there anyway to do this?

5 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 04 Jan 2010, 06:26 AM
Hello Priyanka,

Here's the code to hide the  edit/delete and addnewrecord buttons in your grid:
c#:
protected void RadGrid1_PreRender(object sender, EventArgs e)  
   { 
      if (!Page.User.IsInRole("COMPLIANCE USER"))   
            {                     
               foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)  
               {                 
                  ((LinkButton)dataItem["DeleteColumnUniqueName"].Controls[0]).Enabled = false;   
                  ((LinkButton)dataItem["EditCommandColumnUniqueName"].Controls[0]).Enabled = false;           
  
                  GridCommandItem cmdItem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0];  
                  ((LinkButton)cmdItem.FindControl("InitInsertButton")).Enabled = false;  
                  ((Button)cmdItem.FindControl("AddNewRecordButton")).Enabled = false;              
                } 
            }   
    } 

Thanks
Princy.
0
Eric Klein
Top achievements
Rank 1
answered on 04 Jan 2010, 02:13 PM
Thanks that works great.

0
Eric Klein
Top achievements
Rank 1
answered on 04 Jan 2010, 03:39 PM
I seem to have discovered an issue with this fix.  It works great when there is data for the grid, but when there is no data for the grid the PreRender does not seem to get called.  I have put this code on a few grids and it works when there is data.  I put a break on the prerender and the prerender onlt seems to fire when there is data.  How do I hide the Add New Record if I can't use the preRender?
0
Accepted
Princy
Top achievements
Rank 2
answered on 05 Jan 2010, 01:22 PM
Hello Eric,

The PreRender event of the grid should fire regardless of data present in the grid or not. I'm not really sure of what could be wrong at your end, but the code worked fine on my end.
   
However, another option would be to disable the AddNew Button in the ItemCreated event of the grid as shown below:
c#:
 protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridCommandItem) 
        { 
            GridCommandItem cmdItem = (GridCommandItem)e.Item; 
            ((LinkButton)cmdItem.FindControl("InitInsertButton")).Visible = false
            ((Button)cmdItem.FindControl("AddNewRecordButton")).Visible = false
        } 
    } 

Thanks
Princy.
0
Eric Klein
Top achievements
Rank 1
answered on 05 Jan 2010, 01:25 PM
Thanks again Princy.  I had already changed the add new logic to the item databound and found that it worked.  Not sure why it would not work on PreRender.
Tags
Grid
Asked by
Eric Klein
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Eric Klein
Top achievements
Rank 1
Share this question
or