Error when trying to create custom or user control containing RadListView
Hello,
I am trying to create a control for displaying News items. I have tried to create both a user control and custom control (which extends the SimpleView class). In both instances I get the following error when calling the databind method:
"This control must be hosted by ContentView control or one that derives form it"
I followed the instructions for creating the NewsRotator but instead of using the RadRotator control I used the RadListView control. Here is my intialize method from my custom control where the error occurs:
protected override void InitializeControls(GenericContainer controlContainer)
this.KBList.DataSource = App.WorkWith().NewsItems().Where(n => n.Status == ContentLifecycleStatus.Live).Get().Take(this.kbLimit).ToList();
this.KBList.ItemDataBound += new EventHandler<RadListViewItemEventArgs>(this.KBList_ItemDataBound);
this.KBList.DataBind();
this.KBList.Height = this.Height;
this.KBList.Width = this.Width;
How can I successfully create a user or custom control containing the RadListView control?
Thanks
Dan
Hi Dan,
Try this code
protected
override
void
InitializeControls(GenericContainer container)
NewsList.DataSource = GetNewsListDataSource();
NewsList.PreRender +=
new
EventHandler(NewsList_PreRender);
private
List<NewsItem> GetNewsListDataSource()
return
App.WorkWith()
.NewsItems()
.Publihed()
.OrderByDescending(n => n.PublicationDate)
.Take(100)
.Get()
.ToList();
void
NewsList_PreRender(
object
sender, EventArgs e)
foreach
(var item
in
this
.NewsList.Items)
// put sample control in the template only for the test
var cntrl = item.FindControl(
"Hello1"
)
as
Literal;
if
(item.ItemType == RadListViewItemType.DataItem || item.ItemType == RadListViewItemType.AlternatingItem)
var dataItem = item.DataItem
as
Telerik.Sitefinity.News.Model.NewsItem;
if
(dataItem !=
null
&& cntrl !=
null
)
cntrl.Text = dataItem.Title;
Hi Ivan,
Your code worked....thanks!
Having another issue now with the template. When using the template below, I receive the following exception after the InitializeControls event returns and before the PreRender event handler is called.
Exception of type 'System.Web.HttpUnhandledException' was thrown.
I have isolated the line of code in my template that is causing the issue (control works OK without the line below).
<sf:DetailsViewHyperLink ID="NewsTitle" TextDataField="Title" ToolTipDataField="Description" runat="server" />
Is there any reason why adding the DetailsViewHyperLink control to my template would cause an exception?
Below is the code for my template.
<%@ Control Language="C#" %>
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.ContentUI" Assembly="Telerik.Sitefinity" %>
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.Comments" Assembly="Telerik.Sitefinity" %>
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI" Assembly="Telerik.Sitefinity" %>
<%@ Register TagPrefix="sf" Namespace="Telerik.Sitefinity.Web.UI.PublicControls.BrowseAndEdit" Assembly="Telerik.Sitefinity" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<
p
>
<
telerik:RadListView
ID
=
"KBList"
ItemPlaceholderID
=
"ItemsContainer"
runat
=
"server"
EnableEmbeddedSkins
=
"False"
EnableEmbeddedBaseStylesheet
=
"False"
AllowPaging
=
"True"
>
<
LayoutTemplate
>
<
sf:ContentBrowseAndEditToolbar
ID
=
"MainBrowseAndEditToolbar"
runat
=
"server"
Mode
=
"Add"
></
sf:ContentBrowseAndEditToolbar
>
<
ul
>
<
asp:PlaceHolder
ID
=
"ItemsContainer"
runat
=
"server"
/>
</
ul
>
</
LayoutTemplate
>
<
ItemTemplate
>
<
li
class
=
"sfnewsListItem"
>
<
h2
class
=
"sfnewsTitle"
>
<
asp:Literal
ID
=
"Hello1"
runat
=
"server"
></
asp:Literal
>
</
h2
>
<
h2
>
<
sf:DetailsViewHyperLink
ID
=
"NewsTitle"
TextDataField
=
"Title"
ToolTipDataField
=
"Description"
runat
=
"server"
/>
</
h2
>
<
div
class
=
"sfnewsMetaInfo"
>
<
sf:FieldListView
ID
=
"PublicationDate"
runat
=
"server"
Format
=
"PublicationDate.ToLocal():MMM dd, yyyy"
/>
<
sitefinity:TextField
ID
=
"ViewsCount"
runat
=
"server"
DisplayMode
=
"Read"
/>
</
div
>
<
sf:ContentBrowseAndEditToolbar
ID
=
"BrowseAndEditToolbar"
runat
=
"server"
Mode
=
"Edit,Delete,Unpublish"
></
sf:ContentBrowseAndEditToolbar
>
</
li
>
</
ItemTemplate
>
</
telerik:RadListView
>
Hello Dan,
Please check your support ticket -Customizing the News widget, ID 387036
Regards,
Ivan Dimitrov
the Telerik team
Below is the ticket response I received in case anyone else has a similar issue.
The error that you experience is due to the fact that you use the
DetailsViewHyperLink control. This control can only be used in controls
that are inside a ContentView control(like NewsWidget). The problems is
that DetailsViewHyperLink is trying to get information from the
ContentView control, about how to build the link to the detail
views,e.g. it checks if it should point to the same page or another
page, also some more details about the URL build logic. In your case it
seems that you have implemented the link logic inside the user control,
so you can simply use asp:HyperLink instead of DetailsViewHyperLink.
About extending the views that the NewsWidget can work with-> it is
still possible to add a new type of list view(Implemented as
UserControl) to the NewsWidget through our configuration. It is best if
this new view implements IViewControl, so you can have access to the
settings inside the NewsWidget like filters, default sorting, paging
settings, page where to open the details etc.
To add the new view to the NewsView widget you need to change something
inside the NewsView.config in the App_Data/Sitefinity/Configuration/
This is also going to be possible to do very easy from the UI configuration settings in the next offical release of Sitefinity.
here is how this config looks
<providers>
<add version="4.0.1098.0" name="OpenAccessDataProvider" />
</providers>
///Add after providers section:
<contentViewControls>
<contentViewControl definitionName="NewsFrontend">
<views>
<view viewVirtualPath="~/MyCustomView.ascx" viewName="MyCustomView"
type:this="Telerik.Sitefinity.Web.UI.ContentUI.Config.ContentViewMasterElement,
Telerik.Sitefinity"></view>
</views>
</contentViewControl>
</contentViewControls>