How to hide GridCommandItem button depending on data during load

6 posts, 0 answers
  1. AkAlan
    AkAlan avatar
    121 posts
    Member since:
    Jun 2009

    Posted 13 Jan 2010 Link to this post

    I would like to hide a GridCommandItem button during the grid loading process if a field in the Grid is null. I don't know which event would be best to accomplish this in. Thanks for any help.
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 14 Jan 2010 Link to this post

    Hi AkAlan,

    I guess you want to hide the button placed in CommandItemTemplate if any of the value in column is null. If so you can try the following code snippet in order to achieve this.

        protected void RadGrid1_PreRender(object sender, EventArgs e) 
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
                if (item["Region"].Text == " "// Check for null 
                    GridCommandItem commandItem = (GridCommandItem)RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0]; 
                    Button btn = (Button)commandItem.FindControl("Button4"); 
                    btn.Visible = false

  3. DevCraft R3 2016 release webinar banner
  4. AkAlan
    AkAlan avatar
    121 posts
    Member since:
    Jun 2009

    Posted 14 Jan 2010 Link to this post

    I tried your code (after converting it to vb) and I got an exception saying the index was out of range on this line:

    Dim commandItem As GridCommandItem = DirectCast(RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)(0), GridCommandItem)

    I Do in fact want a button hid but I misspoke in my first post, I want to check the field for not null. I'm going to post more code to make sure I am clear as to what is going on. I'm sure this is simple but I am new to Telerik so thanks for the help.

    Here is the column I want to check during load and if there is a date in the field (indicating the Job is closed), I want to hide the button in the CommandItemTemplate button "btnCloseRwp" that is used to close Jobs.

     <telerik:GridTemplateColumn DataField="DateCompleted"   
                 DataType="System.DateTime" HeaderText="Date C/W"   
                 SortExpression="DateCompleted" UniqueName="DateCompleted"   
                     <asp:TextBox ID="txtDateCompleted" Width="55px" SkinID="XSmallTextBoxLeft" runat="server"   
                         Text='<%# Eval("DateCompleted", "{0:dd MMM,  yy}") %>'></asp:TextBox> 

    Here is the CommandItemTemplate:

           <asp:Button  ID="btnInsertWork"   
                        Text="Add Work" 
           <asp:Button  ID="btnCloseJob"   
                        Text="Close Job" 
  5. Martin
    Martin avatar
    810 posts

    Posted 15 Jan 2010 Link to this post

    Hello Alan,

    You can also wire the ItemDataBound event and set the visibility of the button in the following way:

    Protected Sub RadGrid1_ItemDataBound(ByVal sender As Object, ByVal e As GridItemEventArgs)
            If TypeOf e.Item Is GridDataItem Then
                Dim item As GridDataItem = TryCast(e.Item, GridDataItem)
                Dim textBox As TextBox = TryCast(item.FindControl("txtDateCompleted"), TextBox)
                If textBox.Text.Length <> 0 Then
                    Dim commandItem As GridCommandItem = TryCast(TryCast(sender, RadGrid).MasterTableView.GetItems(GridItemType.CommandItem)(0), GridCommandItem)
                    TryCast(commandItem.FindControl("btnCloseJob"), Button).Visible = False
                End If
            End If
        End Sub

    As to the exception you get - could you verify whether you have set the MasterTableView.CommandItemDisplay property to value different than "none"?

    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  6. Alexander
    Alexander avatar
    14 posts
    Member since:
    Oct 2007

    Posted 15 Nov 2013 Link to this post


    I have a similar need and have not been able to create a solution that is to my satisfaction.

    I want to change the commanditemtemplate content based on status and number of rows in the grid.

    ie. :

    No rows : Only insert command button should be visible, all others invisible.

    Rows exist: Update not visible

    On insert : Only cancel and save visible

    On edit: Delete, Insert, Refresh invisible

    I think you get my drift.

    I have a working solution but it involves setting visibility for commanditems in itemdatabound event when binding GriddataItem.

    I actually would  like to set visibility in declaration as in the following snip:

    <td id="EditCommands" runat="server" class="CommandItems" visible="true">
       <asp:LinkButton ID="btnEditSelectedMD" ToolTip="Edit" runat="server" Visible="false" CommandName="<%#RadGrid.EditSelectedCommandName%>"><img style="border:0px;vertical-align:middle;" alt="" src="img/EditRecord.gif"/></asp:LinkButton>
       <asp:LinkButton ID="btnUpdateEditedMD" runat="server" ToolTip="Save" CommandName="<%#RadGrid.UpdateEditedCommandName%>" Visible='<%# MDGrid.EditIndexes.Count > 0%>'><img style="border:0px;vertical-align:middle;" alt="" src="img/UpdateRecord.gif"/></asp:LinkButton>
       <asp:LinkButton ID="btnCancelMD" runat="server" ToolTip="Cancel" CommandName="<%#RadGrid.CancelCommandName%>" Visible='<%# MDGrid.EditIndexes.Count > 0 Or MDGrid.MasterTableView.IsItemInserted%>'><img style="border:0px;vertical-align:middle;" alt="" src="img/CancelRecord.gif"/></asp:LinkButton>
       <asp:LinkButton ID="btnInsertMD" runat="server" ToolTip="Insert" CommandName="<%#RadGrid.InitInsertCommandName%>" Visible='<%# Not MDGrid.MasterTableView.IsItemInserted And MDGrid.EditIndexes.Count = 0%>'><img style="border:0px;vertical-align:middle;" alt="" src="img/Add1818.png"/></asp:LinkButton>
       <asp:LinkButton ID="btnSaveMD" runat="server" ToolTip="Save" CommandName="<%#RadGrid.PerformInsertCommandName%>" Visible='<%# MDGrid.MasterTableView.IsItemInserted%>'><img style="border:0px;vertical-align:middle;" alt="" src="img/SaveData.gif"/></asp:LinkButton>
       <asp:Image ID="imgSeperatorMD" Style="border: 0px; vertical-align: middle;" alt="" src="img/Separator24.png" runat="server" Visible="false" />
       <asp:LinkButton ID="btnDeleteMD" OnClientClick="javascript:return confirm('Remove document?')" runat="server" ToolTip="Remove document" CommandName="<%#RadGrid.DeleteSelectedCommandName%>" Visible='false'><img style="border:0px;vertical-align:middle;" alt="" src="img/DeleteRecord.gif"/></asp:LinkButton>
       <asp:LinkButton ID="btnRefreshMD" runat="server" ToolTip="Refresh" CommandName="<%#RadGrid.RebindGridCommandName%>" Visible='false'><img style="border:0px;vertical-align:middle;" alt="" src="img/Refresh1616.png"/></asp:LinkButton>

    Here are snips from databound :

    If TypeOf e.Item Is GridDataItem And Not e.Item.IsInEditMode Then
               ' Enable commanditems
               Dim commandItem As GridCommandItem = TryCast(TryCast(sender, RadGrid).MasterTableView.GetItems(GridItemType.CommandItem)(0), GridCommandItem)
               ' Enable editcommands
               TryCast(commandItem.FindControl("btnEditSelectedMD"), LinkButton).Visible = True And (MDGrid.EditIndexes.Count = 0)
               TryCast(commandItem.FindControl("btnDeleteMD"), LinkButton).Visible = True And (MDGrid.EditIndexes.Count = 0)
               TryCast(commandItem.FindControl("btnRefreshMD"), LinkButton).Visible = True And (MDGrid.EditIndexes.Count = 0)
               TryCast(commandItem.FindControl("imgSeperatorMD"), Image).Visible = True And (MDGrid.EditIndexes.Count = 0)

    If TypeOf rpi.DataItem Is GridInsertionObject And e.Item.IsInEditMode Then
        Dim commandItem As GridCommandItem = TryCast(TryCast(sender, RadGrid).MasterTableView.GetItems(GridItemType.CommandItem)(0), GridCommandItem)
        TryCast(commandItem.FindControl("btnEditSelectedMD"), LinkButton).Visible = False
        TryCast(commandItem.FindControl("btnDeleteMD"), LinkButton).Visible = False
        TryCast(commandItem.FindControl("btnRefreshMD"), LinkButton).Visible = False
        TryCast(commandItem.FindControl("imgSeperatorMD"), Image).Visible = False

    Any input on how to improve on this?

    For instance I would need to have number of rows in aspx to get this to work.



  7. Daniel
    Daniel avatar
    4935 posts

    Posted 19 Nov 2013 Link to this post

    Hello Alexander,

    Please try the following approach:
        <asp:LinkButton ID="Button1" runat="server" Text="Refresh" CommandName="Rebind" Visible='<%# DetermineCommandItemButtonVisibility("Rebind") %>' />
        <asp:LinkButton ID="Button2" runat="server" Text="Insert" CommandName="InitInsert"
            Visible='<%# DetermineCommandItemButtonVisibility("InitInsert") %>' />
        <asp:LinkButton ID="Button3" runat="server" Text="Update" CommandName="Update" Visible='<%# DetermineCommandItemButtonVisibility("Update") %>' />
        <asp:LinkButton ID="Button4" runat="server" Text="Cancel" CommandName="Cancel" Visible='<%# DetermineCommandItemButtonVisibility("Cancel") %>' />
        <asp:LinkButton ID="Button5" runat="server" Text="Delete" CommandName="Delete" Visible='<%# DetermineCommandItemButtonVisibility("Delete") %>' />
        <asp:LinkButton ID="Button6" runat="server" Text="Save" CommandName="PerformInsert"
            Visible='<%# DetermineCommandItemButtonVisibility("PerformInsert") %>' />

    protected bool DetermineCommandItemButtonVisibility(string commandName)
        switch (commandName)
            case "Update": return !RadGrid1.MasterTableView.IsItemInserted;
            case "Cancel": return RadGrid1.MasterTableView.IsItemInserted || RadGrid1.EditIndexes.Count > 0;
            case "Refresh": return RadGrid1.MasterTableView.Items.Count > 0;
            case "Delete": return !RadGrid1.MasterTableView.IsItemInserted && RadGrid1.EditIndexes.Count == 0;
            case "InitInsert": return !RadGrid1.MasterTableView.IsItemInserted;
            case "PerformInsert": return RadGrid1.MasterTableView.IsItemInserted;
        return true;

    I hope this helps.

    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 the blog feed now.
Back to Top
DevCraft R3 2016 release webinar banner