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

Not able to Enable/Disable commanditem buttons of telerik radgrid (telerik version 2008.3.1314.20)

1 Answer 24 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sankeerthi
Top achievements
Rank 1
Sankeerthi asked on 02 Mar 2016, 11:34 AM

I have radgrid, with 3 command buttons(Edit, Save and Cancel)

On Edit click Save and cancel buttons should be visible. (By default they should be disabled)

after Save or Cancel click grid goes to read only mode and only Edit command button should be displayed.

i tried all the possibilities by finding the command button and disabling it. Though there is no exception or anything still couldn't achieve this functionality.

Please help me out..it is needed immediately.

CODE :

  <telerik:RadGrid ID="gridMilitaryPension" runat="server" AutoGenerateColumns="false" AllowMultiRowEdit="true"  RenderMode="lightweight"
                     Skin="Office2007" PagerStyle-Mode="NextPrevAndNumeric" AllowPaging="True" PageSize="30" OnNeedDataSource="gridMilitaryPension_NeedDataSource"
                      OnItemCommand="gridMilitaryPension_ItemCommand" >
                        <MasterTableView CommandItemDisplay="Top" EditMode="InPlace" Font-Size="10pt">                          
                            <Columns>                           
                             
                                 <telerik:GridBoundColumn UniqueName="ID" DataField="ID" HeaderText="ID" Display="true" ReadOnly="True"
                                    ForceExtractValue="Always">
                                </telerik:GridBoundColumn>

                                 <telerik:GridBoundColumn UniqueName="slottype" DataField="slottype" HeaderText="Slot Type" SortExpression="slottype" ReadOnly="True"
                                    ForceExtractValue="Always">
                                </telerik:GridBoundColumn>
                                 
                                 <telerik:GridTemplateColumn DataField="total" UniqueName="total" HeaderText="Total Seats" SortExpression="total">
                                    <ItemTemplate>
                                        <asp:Label ID="lblTotalSeats" runat="server" Text='<%# Eval("total") %>' class="classLblTotal"></asp:Label>
                                      
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="txtTotalSeats" runat="server" Text='<%# Eval("total") %>' Width="100%"  class='<%# Eval("slottype") %>'  onblur="addition(this)" onchange="DataChanged(this)"></asp:TextBox>
                                    </EditItemTemplate>
                                 </telerik:GridTemplateColumn>

                            </Columns>
                          
                              <CommandItemTemplate>
                                  <table>
                                      <tr>                                     
                                         <td> <asp:Button runat="server" ID="btnSaveAllChanges" Text="Save All Changes"  CommandName="SaveAllChanges"  /></td>
                                         <td> <asp:Button runat="server" ID="btnCancel" Text="Cancel"  CommandName="Cancel" /></td>
                                         <td> <asp:Button runat="server" ID="btnEdit" Text="Edit Total Seats" CommandName="Edit"  onclick="btnEdit_Click"/></td>
                                       </tr>

                                  </table>
                                 
                              </CommandItemTemplate>
                           
                        </MasterTableView>
                      
                    
                    </telerik:RadGrid>

 

 

In code Behind:

 protected void gridMilitaryPension_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
           
            if (e.CommandName == "Edit")
            {

//Below i am capturing the controls and trying to hide/unhide. used visible property also but no luck
                GridCommandItem cmditem = (GridCommandItem)e.Item;
                Button btnSaveAllChanges = cmditem.FindControl("btnSaveAllChanges") as Button;
                Button btnCancel = cmditem.FindControl("btnCancel") as Button;
                Button btnEdit = cmditem.FindControl("btnEdit") as Button;
                btnSaveAllChanges.Enabled = true;
                btnCancel.Enabled = true;
                btnEdit.Enabled = false;            
             
                }                
            }
            if (e.CommandName == "SaveAllChanges")
            
            {
               //after logic, Edit button should be enabled.
                  
                }

             
                return;
            }
            if (e.CommandName == "Cancel")
            {
               
                gridMilitaryPension.MasterTableView.ClearEditItems();

                gridMilitaryPension.Rebind();  
            }
        }     

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 07 Mar 2016, 08:01 AM
Hi Sankeerthi,

The ItemCommand event fires for GridDataItem objects and in order to get reference to the GridCommandItem you should use the following line instead:
GridCommandItem cmditem = gridMilitaryPension.MasterTableView.GetItems(GridItemType.CommandItem)[0] as GridCommandItem;

As for enabling/disabling the buttons within the GridCommandItem you should do that within the PreRender event of the grid or within the OnItemCreated/OnItemDataBound events, because after OnItemCommand event fires, the RadGrid will rebind and you will lose the customization over the buttons.

Please note that you may have to use global flags to determine whether to disable or enable the buttons, where the flags could be set in your custom events.

Finally, keep in mind that version 2008 is not supported and it is highly recommended that you upgrade to newer version.

Hope this helps.


Regards,
Konstantin Dikov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Sankeerthi
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or