accessing template control properties in code behind.

N_K

Member
Joined
Oct 10, 2006
Messages
22
Programming Experience
3-5
Hi,

I'm making a web control which is a table of a few contacts, each contact row having a button to view or edit the contact depending on permissions allowed.

The whole roles and permissions bit is sorted. what my problem is is accessing the visible property of the buttons on the control (well they are images with hyperlinks really.)

Within the Datalist and tables in the control I have a hyperlink and image for each 'button'. My code is supposed to test whether a Boolean (the current value is hardcoded during development) is tru or not and manipulate the visible property of the ViewImage and ViewLink controls respectively.

For some reason ViewLink and ViewImage will not be recognised by the compiler. Does anyone understand why?

My code is below.

The Code for my control is here:
VB.NET:
<asp:DataList ID="ContactsDataList" runat="server" Width="98%">
    <HeaderTemplate>        
    <asp:Table ID="AddContactTable" runat="server" CssClass="iconcontainer">
            <asp:TableRow>
                <asp:TableCell>
                    <asp:HyperLink ID="AddLink" runat="Server" NavigateUrl="#" ><asp:Image ID="AddImage" runat="server" ImageUrl="~/Common/Images/layout/Add.gif" AlternateText="Add Contact" /></asp:HyperLink>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <asp:Table runat="server" ID="ContactsTableTop" CssClass="singletabletops">
            <asp:TableRow>
                <asp:TableCell><strong>Contacts</strong></asp:TableCell> 
                <asp:TableCell>results:</asp:TableCell>
                <asp:TableCell>jump to:</asp:TableCell>
            </asp:TableRow>
        </asp:Table>
        <table class="detail">
        <tr class="gridbodytr">
            <th class="gridbodyth">Contact:</th>
            <th class="gridbodyth">Tel:</th>
            <th class="gridbodyth">Detail:</th>
        </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr class="gridbodytr">
            <td class="gridbodytd"><asp:Label ID="ContactName" runat="server" Text=""><%#Eval("Forename") & " " & Eval("Surname")%></asp:Label></td>
            <td class="gridbodytd"><asp:Label ID="ContactTel" runat="server" Text=""><%#Eval("Telephone1")%></asp:Label></td>
            <td class="gridbodytd" nowrap="nowrap">
                <asp:HyperLink ID="ViewLink" runat="server" NavigateUrl="#"><asp:Image ID="ViewImage" runat="server" ImageUrl="~/Common/Images/layout/view.gif" AlternateText="View Contact" /></asp:HyperLink>
                <asp:HyperLink ID="EditLink" runat="server" NavigateUrl="#"><asp:Image ID="EditImage" runat="server" ImageUrl="~/Common/Images/layout/Edit.gif" AlternateText="Edit Contact" /></asp:HyperLink>
                <asp:HyperLink ID="DeleteLink" runat="server" NavigateUrl="#"><asp:Image ID="DeleteImage" runat="server" ImageUrl="~/Common/Images/layout/Delete.gif" AlternateText="Delete Contact" /></asp:HyperLink>      
            </td>
        </tr>
         
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>   
    
</asp:DataList>

VB.NET:
Imports CommonDAL1TableAdapters
Imports Address


Partial Class Controls_Address_Contacts
    Inherits System.Web.UI.UserControl

    Dim AddressType As String
    Dim ReferenceId As Integer
    'Dim RoleAccess As Object = CType(Session("UsersAccess"), Object)
    Dim ViewButton = ContactsDataList.FindControl("viewimage")

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

    End Sub

    Public Sub GenerateDataList( _
            ByVal AddressType As String, _
            ByVal ReferenceId As Integer, _
            ByVal ObjectId As Integer)

        'Declare a new Address Object
        Dim Contacts As New Address
        Dim View As Boolean = False

        ' Set Source and bind the DataGrid

        ContactsDataList.DataSource = Contacts.SelectAddressByRefAndType(AddressType, ReferenceId)
        ContactsDataList.DataBind()

        ' Check Role permissions
        If View Then

            ViewLink.Visible = True
            ViewImage.Visible = True
        Else
            ViewLink.Visible = False
            ViewImage.Visible = False

        End If

    End Sub

End Class
 
You can do the View evaluation in the datalists ItemDataBound event:
VB.NET:
Protected Sub ContactsDataList_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) _
Handles ContactsDataList.ItemDataBound
   Dim x As Control = e.Item.FindControl("ViewLink")
   If Not x Is Nothing Then x.Visible = False
End Sub
 
Thank you.

I'm afraid that isnt the right thing I'm trying to do.

I want to access the ViewLink on each repetition/row and change the ViewLink.Visible property depending on a boolean I will obtain from the session. (for now the variable View is used and set to false).

so rather than finding out what the current visibility of the ViewLink I want to access it and change its visibility. VS doesnt seem to recognise the ViewLink element despite it being in the mark up file for the control.

Does that make sense?

I appreciate any help you could give me.

beN
 
The code I gave you does exactly that, if you had tried it you would have seen that the ViewLink control was found one time for each data item and that this controls Visible was set to False. What code you do to determine the True/False value is up to you. It looks as you are supposed to do a user check that will determine visible fields for the whole page, so I would have done this check in for example page Init event once and just used the Boolean value in ItemDataBound event (rather than doing the user check for each data item)
 
Similarly I'm now attempting to access the Hyperlink url for each record in a similar way however I can't seem to access that.

any pointers?

my code is below.

VB.NET:
    Protected Sub ContactsDataList_ItemDataBound( _
        ByVal sender As Object, _
        ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) _
        Handles ContactsDataList.ItemDataBound

        Dim ViewControl As Control = e.Item.FindControl("ViewLink")
        Dim AddControl As Control = e.Item.FindControl("AddLink")

        Dim HView As Control = e.Item.FindControl("ViewLink")


        ' Check if the Control exists
        If Not ViewControl Is Nothing Then
            ' Show 'Add' button if role allows
            If ViewAllowed Then
                ViewControl.Visible = True
                HView.NavigateUrl = "http://www.bbc.co.uk"
            Else
                ViewControl.Visible = False
            End If
        End If

        ' Check if the role exists
        If Not AddControl Is Nothing Then
            ' Show 'Add' button if role allows
            If Addallowed Then
                AddControl.Visible = True
            Else
                AddControl.Visible = False
            End If
        End If

    End Sub
 
Cast the control you search/found to the specific type control, in this case it is a HyperLink control (and you know it already when searching for it).
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] x [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] HyperLink = [/SIZE][SIZE=2][COLOR=#0000ff]DirectCast[/COLOR][/SIZE][SIZE=2](e.Item.FindControl([/SIZE][SIZE=2][COLOR=#800000]"ViewLink"[/COLOR][/SIZE][SIZE=2]), HyperLink)[/SIZE]
With 'x' now as HyperLink instead of generic Control you can use the NavigateUrl property.
 
thanks.

How do I refer to fields in the DataList directly in this code. I want to build a query string in the url to feature variables taken from the current record. I want to add the field "AddressBookId" for example to the query string. So far I have:

VB.NET:
[SIZE=2]
HView.NavigateUrl = [/SIZE][SIZE=2][COLOR=#800000]"~/Controls/Address/ContactDetailsForm.aspx?Size=Contact&AddressBookId="[/COLOR][/SIZE][SIZE=2] & ContactsDataList.Items([/SIZE][SIZE=2][COLOR=#800000]"AddressBookId"[/COLOR][/SIZE][SIZE=2])
[/SIZE]

but alas i'm a little stuck.
 
You can cast the e.Item.DataItem to a DataViewRow and get the field from there:
VB.NET:
Dim str As String = CType(e.Item.DataItem, Data.DataRowView).Item("AddressBookId")
 
Back
Top