How to extend sf:DetailsViewHyperLink
Hi,
I want to customise the News Widget by extending the sf:DetailsViewHyperLink to make it point to a custom url (mainly an onClick event to do some javascript processing).
I was looking over Extending MasterViewHyperLink but I can't create any class in the App_Data folder on my website from VS, also, I don't see how to register this class and use it after that.
I appreciate your help,
Thanks
Hi martani,
I am not sure why you are trying to add a class inside App_Data folder, since you should use class library. You should inherit from DetailsViewHyperLink and override OnDataBinding method of the class. There you can set the NavigateUrl for each item.
sample
protected
override
void
OnDataBinding(EventArgs e)
base
.OnDataBinding(e);
var container =
this
.GetDataItemContainer();
object
dataItem =
null
;
if
(container ==
null
&&
this
.DataItem !=
null
)
dataItem =
this
.DataItem;
if
(container ==
null
&&
this
.DataItem ==
null
)
throw
new
InvalidOperationException(
"This control can be used only within a data bound item template."
);
if
(container !=
null
)
dataItem = container.DataItem;
if
(dataItem !=
null
)
var host =
this
.GetHostControl<ContentView>();
if
(host ==
null
)
throw
new
InvalidOperationException(
"This control must be hosted by ContentView control or one that derives form it."
);
var detailsId = host.MasterViewDefinition.DetailsPageId;
if
(detailsId != Guid.Empty)
this
.NavigateUrl =
""
;
else
this
.NavigateUrl =
""
;
if
(!String.IsNullOrEmpty(
this
.TextDataField))
this
.Text = DataBinder.Eval(dataItem,
this
.TextDataField,
"0"
);
if
(!String.IsNullOrEmpty(
this
.ToolTipDataField))
this
.ToolTip = DataBinder.Eval(dataItem,
this
.ToolTipDataField,
"0"
);
Thank you,
It works very well, my first extension to Sitefinity that worked :)
Can anyone explain this part a little better, I get the other part using the custom class, "Then you can modify the template of the NewsView control from the control designer and replace the default DetailsViewHyperLink with your custom one. Your custom control should have the same ID as the default control - "DetailsViewHyperLink"
But is the template now referencing a new custom user control?
Thanks,
-Ben
Ok I figured it out, you have to include a reference to the class in the widget template, so in my case I have a class called EventTypeDetailsViewHyperlink in a Classes folder off the Sitefinity root. In my template, I'm using the refrence <%@ Register TagPrefix="custom" Namespace="SitefinityWebApp.Classes" Assembly="SitefinityWebApp" %>
Then when I declare the control I basically just copied the existing details markup but changed the prefix and the name to match the class so: <custom:EventTypeDetailsViewHyperlink ID="DetailsViewHyperLink" Text="View Details" runat="server" data-sf-field="Title" data-sf-ftype="ShortText" />
-Ben