Disable a user control telerik:RadButton inside telerik:RadListView ItemTemplate on code behind

220 Views Asked by At

I have a user control named "UserControl_Attachments".

<%@ Register TagPrefix="UserControl_Attachments" TagName="Attachments" Src="/UserControls/UserControl_Attachments.ascx" %>

<UserControl_Attachments:Attachments ID="Attachments_Bulletin" runat="server" />

Inside the user control, I have a telerik:RadListView and in the "ItemTemplate" I have a telerik:RadButton with an ID="UC_DeleteAttachment_Button". Depending on the user login from the code behind I want to hide the button.

<telerik:RadListView runat="server" ID="UC_CurrentAttachments" AllowPaging="true" PageSize="40" OnNeedDataSource="UC_CurrentAttachments_NeedDataSource">

            <LayoutTemplate>
                <div style="max-width: 800px">
                    <asp:Panel ID="itemPlaceholder" runat="server">
                        <table>
                        </table>
                    </asp:Panel>
                </div>
            </LayoutTemplate>

            <ItemTemplate>
                <tr>
                    <td>
                        <a href="<%# Eval("AttachmentPath") %>" target="_blank">
                            <img src="<%# Eval("ImagePath") %>" width="<%# Eval("DisplayWidth") %>" height="<%# Eval("DisplayHeight") %>" />
                        </a>
                    </td>
                    <td>
                        <asp:Label runat="server" ID="Label_AttachmentName" Text='<%# Eval("DisplayName") %>' Font-Size="10px" />
                    </td>
                    <td>
                        <telerik:RadButton ID="UC_DeleteAttachment_Button" Width="25" runat="server" OnClick="UC_DeleteAttachment_Click" OnClientClicking="UC_Attachment_DeleteConfirm">
                            <Icon PrimaryIconCssClass="rbRemove"></Icon>
                        </telerik:RadButton>
                    </td>
                </tr>

                <asp:HiddenField ID="HiddenField_AttachmentPath" runat="server" Value='<%# Eval("AttachmentPath") %>' />

            </ItemTemplate>

        </telerik:RadListView>

Here is my code behind:

var CurrentAttachment_UC = (RadListView)Attachments_Bulletin.FindControl("UC_CurrentAttachments");        
var CurrentAttachmentDeleteButton = (RadButton)CurrentAttachment_UC.FindControl("UC_DeleteAttachment_Button");

if (CurrentAttachmentDeleteButton != null)
        CurrentAttachmentDeleteButton.Visible = false;

I've tried several things and still I can't find the control. It is always null. Is there anyway I can hide the button? Thanks in advance.

1

There are 1 best solutions below

0
Hounddog75 On

The best practise for finding a control within a RadListView is to use the ItemCreated event or ItemDataBound event for the RadListView:

Protected Sub RadListView1_ItemCreated(sender As Object, e As RadListViewItemEventArgs) Handles RadListView1.ItemCreated

    If TypeOf e.Item Is RadListViewItem Then
        Dim btn As RadButton= CType(e.Item.FindControl("UC_DeleteAttachment_Button"), RadButton)
        btn.Disabled = True
    End If

End Sub