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

Hide CommandItem link on hiddenfield in grid

2 Answers 82 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 04 Nov 2014, 06:30 PM
I am trying to hide a commandItem link button based on a hiddenfiled that is in a template column inside the grid.  It keeps giving me a null value because I am assuming the grid head loads beofre the body.  So how can I hide a command item from a row in the grid.

  <CommandItemTemplate>
                                                     <asp:LinkButton ID="lnkAddDate" Text="Add Dates" CommandName="InitInsert" runat="server"></asp:LinkButton>
                                                </CommandItemTemplate>
                                                <Columns>
                                                    <telerik:GridTemplateColumn>
                                                        <ItemTemplate>
                                                            <asp:LinkButton ID="lnkDelDate" runat="server" CommandName="DelDate" CommandArgument='<%# Bind("intLeaseDateId")%>'>Delete</asp:LinkButton>
                                                            <asp:HiddenField ID="HFOccur" runat="server" Value='<%# Bind("intOcurranceId")%>' />
                                                        </ItemTemplate>
                                                    </telerik:GridTemplateColumn>
                                                    <telerik:GridEditCommandColumn HeaderText="Edit" UniqueName="EditGrid" />
                                                    <telerik:GridBoundColumn HeaderText="Start Date" DataField="dtStartDate" />
                                                    <telerik:GridBoundColumn HeaderText="End Date" DataField="dtEndDate" />
                                                </Columns>
 
 
I can find it here to hide a linkbutton that is a column in the grid, but I cannot get at the commanditem button.
 
  If (TypeOf e.Item Is GridDataItem AndAlso e.Item.OwnerTableView.Name = "LeaseDates") Then
            Dim item As GridDataItem = CType(e.Item, GridDataItem)
            Dim delete As LinkButton = DirectCast(item.FindControl("lnkDelDate"), LinkButton)
            Dim occur As HiddenField = DirectCast(item.FindControl("HFOccur"), HiddenField)
            Dim status As Integer = 2
 
            If Convert.ToInt32(occur.Value) = 5 Then
                delete.Visible = False
            End If
        End If
 
 
Atempt at the commanditem from other sources on web, but does not work.
  Protected Sub myradGrid_ItemCreated(sender As Object, e As GridItemEventArgs) Handles myradGrid.ItemCreated
        If (TypeOf e.Item Is GridCommandItem AndAlso e.Item.OwnerTableView.Name = "LeaseDates") Then
            Dim cmdItem As GridCommandItem = CType(e.Item, GridCommandItem)
            Dim btn As LinkButton = DirectCast(cmdItem.FindControl("lnkAddDate"), LinkButton)
            'Dim occur As HiddenField = DirectCast(cmdItem.FindControl("HFOccur"), HiddenField)
            'Response.Write(occur.Value)
            'Response.End()
            'If Convert.ToInt32(occur.Value) = 5 Then
            '    btn.Visible = False
            'End If
        End If
    End Sub

2 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 1
answered on 04 Nov 2014, 06:40 PM
I forgot to mention that this is a sub grid of the main grid in a heiarchy mode.
0
Eyup
Telerik team
answered on 07 Nov 2014, 01:23 PM
Hello Kevin,

You can use the GetItems method to achieve the requested functionality:
http://www.telerik.com/help/aspnet-ajax/grid-using-getitems-getcolumn-methods.html

Alternatively, in you can also use the PreRender event handler of the add button:
Protected Sub lnkAddDate_PreRender(sender As Object, e As EventArgs)
    Dim button As LinkButton = sender
    Dim commandItem As GridCommandItem = button.NamingContainer
    If GetHiddenFieldCondition(commandItem.OwnerTableView) Then
        button.Visible = False
    End If
End Sub
Private Function GetHiddenFieldCondition(gridTableView As GridTableView) As Boolean
    For Each item As GridDataItem In gridTableView.Items
        'here check for HiddenField value
        Dim condition As Boolean = True
        If condition Then
            Return True
        End If
    Next
    Return False
End Function

Hope this helps. Please give it a try and let me know if it works for you.

Regards,
Eyup
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or