Monday, November 3, 2008

Using the PageLink custom server control

This is the second in a series of posts on the new custom server controls that were released in v5.5 of NetCommunity. As I stated in the previous post, custom server controls allow us to take canned pieces of functionality and include them in our own custom parts. In this post I'm going to touch on using the PageLink control. This control allows the user to select a link. It shows the link currently selected and offers the standard modal dialog for selecting a link either from the site, to a document, or a manually entered URL. You'll be able to download the Visual Studio project at the end of this post.

I'm going to assume that you are familiar with the concept of how to build and implement custom parts in NetCommunity. If you aren't, don't worry, just take a few minutes to check out the developer resources before getting started.

PageLinkControlEditor.ascx (Design View)

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="PageLinkControlEditor.ascx.vb" Inherits="PageLinkControl.PageLinkControlEditor" %>
<%@ Register TagPrefix="bbnc" Namespace="BBNCExtensions.ServerControls" Assembly="BBNCExtensions" %>

Please select a page: <bbnc:pagelink runat="server" id="PageLink1"></bbnc:pagelink>
Please enter the name for the link: <asp:textbox runat="server" id="txtPageLinkName"></asp:textbox>
Here we are registering the BBNCExtensions.ServerControls with the tag "bbnc".
Then we include the control <bbnc:pagelink runat="server" id="PageLink1">
Also we are allowing the name of link on the front end to be specified in txtPageLinkName.

PageLinkControlEditor.vb (Code Behind)

Imports BBNCExtensions

Partial Public Class PageLinkControlEditor
Inherits BBNCExtensions.Parts.CustomPartEditorBase

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'not really used for editors, the OnLoadContent() handles this...
End Sub

#Region " Editor Specific Sub "
Public Overrides Sub OnLoadContent()
If Not Page.IsPostBack Then
Dim oProperties As Properties
oProperties = CType(Me.Content.GetContent(GetType(Properties)), Properties)
If Not oProperties Is Nothing Then
PageLink1.PageID = oProperties.PageLinkID
txtPageLinkName.Text = oProperties.PageLinkText
End If
End If
End Sub
Public Overrides Function OnSaveContent(Optional ByVal bDialogIsClosing As Boolean = True) As Boolean
Dim oProperties As New Properties
With oProperties
.PageLinkText = txtPageLinkName.Text
.PageLinkID = PageLink1.PageID
End With
Me.Content.SaveContent(oProperties)
Return True
End Function
#End Region

End Class
In the OnSaveContent() we take the ID of the selected page using PageLink1.PageID and save it to oProperties.PageLinkID.
We are also going to save the text for the link name using txtPageLinkName.Text to oProperties.PageLinkText.

The OnLoadContent() we load up any previously saved Properties and apply it to the respective controls.

Properties.vb (Editor Properties)

Public Class Properties
Public PageLinkText As String
Public PageLinkID As String
End Class
Here we dim out the Properties the Editor and the Display will be using.

PageLinkControlDisplay.ascx (Design View)

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="PageLinkControlDisplay.ascx.vb" Inherits="PageLinkControl.PageLinkControlDisplay" %>
<:asp:hyperlink runat="server" id="hypPageLink"></asp:hyperlink>
Nothing fancy here, just declaring the HyperLink control we'll be working with in the code behind.

PageLinkControlDisplay.vb (Code Behind)

Imports BBNCExtensions

Partial Public Class PageLinkControlDisplay
Inherits BBNCExtensions.Parts.CustomPartDisplayBase

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
getProperties()
End Sub

#Region " Data Handling "
Private Sub getProperties() 'this loads up the properties from the editor
Dim oProperties As Properties 'class contating the availible properties
oProperties = CType(Me.Content.GetContent(GetType(Properties)), Properties) 'this is a BBNC method
If Not oProperties Is Nothing Then 'don't try to assign the properties to the display controls if there aren't any returned
hypPageLink.NavigateUrl = API.Pages.BuildStandardLink(CType(oProperties.PageLinkID, Integer)) 'this bit here basically takes the PID and then dynamically builds the URL for your specific website.
hypPageLink.Text = oProperties.PageLinkText
End If
End Sub
#End Region

End Class

Again, nothing really fancy. We are retrieving the Properties saved in the Editor using oProperties = CType(Me.Content.GetContent(GetType(Properties)), Properties).
Then we want to apply the Properties to the hypPageLink control. The important piece here is API.Pages.BuildStandardLink(CType(oProperties.PageLinkID, Integer)). It is a native function in the v5.5 API that takes the saved PID (page ID) from the Properties and returns a full URL for that page.
We are also applying the oProperties.PageLinkText to the hypPageLink.Text.

Download this project.

That's it! Pretty simple. If you want me to showcase a specific control please send me an email.
If you haven't already, follow me on Twitter and join our FaceBook group.

No comments: