+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / How to extend sf:DetailsViewHyperLink

How to extend sf:DetailsViewHyperLink

5 posts, 1 answered
  1. martani
    martani avatar
    27 posts
    Registered:
    24 Jan 2011
    10 Feb 2011
    Link to this post
    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
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    10 Feb 2011
    Link to this post
    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}");
        }
     
    }

    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"

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
  3. martani
    martani avatar
    27 posts
    Registered:
    24 Jan 2011
    15 Feb 2011
    Link to this post
    Thank you,

    It works very well, my first extension to Sitefinity that worked :)
  4. Ben
    Ben avatar
    44 posts
    Registered:
    27 Sep 2012
    22 Nov 2013
    Link to this post
    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
  5. Ben
    Ben avatar
    44 posts
    Registered:
    27 Sep 2012
    22 Nov 2013
    Link to this post
    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

5 posts, 1 answered