How to access server control in DynamicDetailContainer layout template
I have an .ascx user control and I am wanting to grab a value from a server control that exists within the layout template for a sf:DynamicDetailContainer (see <
asp:HiddenField
runat
=
"server"
ID
=
"hdnFullname"
Value='<%# Eval("FullName")%>' /> below)
. I have tried using .findcontrol as shown below on the databound event for the detailContainer with no luck. How do I accomplish this?
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="OpenAccessDataProvider,d1f6cf910ef64f2eaa0dba8830221137.ascx.vb"
Inherits="SfCtrlPresentation_OpenAccessDataProvider_d1f6cf910ef64f2eaa0dba8830221137" %>
<%@ Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.DynamicModules.Web.UI.Frontend"
TagPrefix="sf" %>
<%@ Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI.Fields"
TagPrefix="sf" %>
<%@ Register Assembly="Telerik.Sitefinity" Namespace="Telerik.Sitefinity.Web.UI"
TagPrefix="sf" %>
<
sf:DynamicDetailContainer
ID
=
"detailContainer"
runat
=
"server"
>
<
LayoutTemplate
>
<
div
class
=
"professional photo float-left img-frame"
>
<
div
>
<
img
src='<%# "http://cdn2.sva.com/photos/" + Eval("FirstName") + "-" + Eval("LastName") + ".jpg"%>' alt='<%# Eval("FullName")%>' class="img-background" /></
asp:HyperLink
>
</
div
>
</
div
>
<
ul
class
=
"professional bio float-left"
>
<
li
>
<
h2
>
<%# Eval("FullName")%>
<
asp:HiddenField
runat
=
"server"
ID
=
"hdnFullname"
Value='<%# Eval("FullName")%>' />
</
h2
>
</
li
>
<
li
>
<
h3
>
<%# Eval("Title")%>
</
li
>
</
h3
>
<
li
>
<%# Eval("PhoneNumber")%>
| <
a
href='<%# "mailto:" + Eval("EmailAddress")%>'>
<%# Eval("EmailAddress")%></
a
></
li
>
<
li
class
=
"clear"
>
<
img
src
=
"http://cdn3.sva.com/img/icons/vCard.gif"
alt
=
"Download vCard"
class
=
"float-left"
/>
<
asp:HyperLink
runat
=
"server"
ID
=
"lnkVCard"
Text
=
"Download vCard"
NavigateUrl='<%# "~/tools/vcardhandler.ashx?
lname
=
" + Eval("
LastName") + "&
fname
=
" + Eval("
FirstName") + "&
title
=
" + Eval("
Title") + "&
organization
=
SVA
" + "&
phone
=
" + Eval("
PhoneNumber") + "&
email
=
" + Eval("
EmailAddress") + "&
photo
=
" + Eval("
FirstName") + "-" + Eval("LastName") + ".jpg" %>'></
asp:HyperLink
>
</
li
>
<
asp:Panel
runat
=
"server"
ID
=
"pnlLinkedIn"
Visible
=
"false"
>
<
li
class
=
"clear"
>
<
img
src
=
"http://cdn3.sva.com/img/icons/LinkedIn.gif"
alt
=
"LinkedIn Profile"
class
=
"float-left"
/>
<
asp:HyperLink
runat
=
"server"
ID
=
"lnkLinkedIn"
NavigateUrl='<%# Eval("LinkedIn")%>'
ToolTip='<%# "Visit " + Eval("FirstName") + " on LinkedIn" %>' Text='<%# "Visit " + Eval("FirstName") + " on LinkedIn" %>'></
asp:HyperLink
></
li
></
asp:Panel
>
</
ul
>
<
h2
class
=
"clear"
>
More about
<%# Eval("FirstName") %></
h2
>
<
div
>
<%# Eval("Bio")%></
div
>
</
LayoutTemplate
>
</
sf:DynamicDetailContainer
>
Protected Sub detailContainer_DataBound(sender As Object, e As System.EventArgs) Handles detailContainer.DataBound
Dim hdnFullname As HiddenField = Me.detailContainer.FindControl("hdnFullname")
End Sub
If this can't be done, can I use a different kind of databound control I can accomplish this with, in place of the sf:DynamicDetailContainer?
Hello Craig,
The DynamicDetailContainer is a template control, which means that you need to have an Item template also. The Layout template should only specify how the layout of the control would look, but the Item template should indicate where exactly the bindings should happen (an example of this can be seen in our widget templates). Then, since you have the same controls for each different item that is bound to the DynamicDetailContainer, you need to wire not to DataBound but to ItemDataBound event, because for each bound item, you will have a different HiddenField control and when you try to find it with FindControl, it won't succeed, as it is not clear exactly which HiddenField you want to select.
Overall, can you tell me what is your end purpose, so I can suggest a better way to achieve it, if there is one?
Hi Svetoslav,
What I have done is create a custom module in SF4.4 called "Professionals". This is where I show a list of professionals, and their corresponding bio's, in my company to appear on a Professionals Directory page of our website, www.sva.com/.../professionals. I have some professionals who have video links associated with their bios, as well as LinkedIn.com bio links. Because not all professionals in the directory will have these links, I need to be able to conditionally hide or show these links in the bio template depending on whether or not they have one.
The code I posted in my first post is the single-item template code for my custom Professionals module. I am overriding the template shown in the backend of my Sitefinity site by placing a .net usercontrol in the SfCtrlPresentation folder as instructed in another forum post. All of this works fine, I just can't figure out how to programmatically access specific asp panels / controls inside the Dynamic <sf:DynamicDetailContainer> layout template via code-behind for the usercontrol to conditionally show the above mentioned data.
Can you provide me with a code sample demonstrating what you are talking about?
Hello Craig,
I understand what is the problem now (I see that Josh instructed you how to map an external template to the dynamic content control). However, doing it this way, some of the events may not be hit, as the template would be in the database already when you wire it up to the code-behind (and consequently you won't be able to access the controls on the template). When you debug the code-behind, does the DataBound handler get hit when your control loads on the page?
All the best,Hi Svetoslav,
Yes, the detailContainer_DataBound event is hit. I can put test code in there to response.write out a random string, for example. I cannot however find controls in the LayoutTemplate of this detail container control as show below in my original post.
If it is not possible for me to access controls inside the LayoutTemplate in this fashion, how else would you suggest I approach the functionality needed to conditionally show or hide controls and panels as discussed below?
Hello Craig,
I was able to get the controls (for example the FieldListView control) in the LayourTemplate of the DynamicDetailContainer like this:
((DetailItem)
this
.detailContainer.Controls[0]).FindControl(
"PublicationDate"
);
Hi Svetoslav,
This worked! Thank you so much for all your help.