Change edit mode on load

6 posts, 2 answers
  1. Eric Klein
    Eric Klein avatar
    183 posts
    Member since:
    Aug 2009

    Posted 31 Dec 2009 Link to this post

    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?
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 04 Jan 2010 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Eric Klein
    Eric Klein avatar
    183 posts
    Member since:
    Aug 2009

    Posted 04 Jan 2010 Link to this post

    Thanks that works great.

  5. Eric Klein
    Eric Klein avatar
    183 posts
    Member since:
    Aug 2009

    Posted 04 Jan 2010 Link to this post

    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?
  6. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 05 Jan 2010 Link to this post

    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.
  7. Eric Klein
    Eric Klein avatar
    183 posts
    Member since:
    Aug 2009

    Posted 05 Jan 2010 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017