Tuesday, September 28, 2010

 

Binding a Custom Meta field to a Template in Sitefinity with a ASP.NET Usercontrol

To get the background about this issue please read this article in Sitefinity knowledge base.

The last section of the article suggests that you can use the following syntax to set the NavigateUrl property of the ASP.NET Hyperlink control with its Text property

<asp:HyperLink ID="PDFAttachment" runat="server" NavigateUrl='<%=this.Text %>'></asp:HyperLink>  
 

However the above doesn’t work and <%= %> is rendered as plain text


After trying to figure this out for a while I found the following way to do this, the trick is to create a custom user control that implements ITextControl interface, like this



public partial class UserControls_PDFAttachmentDownload : System.Web.UI.UserControl, ITextControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
 
    private string text;
 
    public string Text
    {
        get
        {
            return this.text;
        }
        set
        {
            if (!string.IsNullOrEmpty(value))
            {
                this.showdownload.NavigateUrl = ResolveUrl(value);
            }
        }
    }
}

The highlighted line above sets the NavigateUrl property from the Text value that is bound to the control.


The ASCX part of the user control simply has a ASP.NET Hyperlink control in it



<asp:HyperLink ID="showdownload" runat="server" Text="Download" />

To use this control just add a reference in the Template usercontrol that Sitefinity uses and your on your way.



<%@ Register TagName="Attachment" TagPrefix="PDF" Src="~/UserControls/PDFAttachmentDownload.ascx" %>
<div class="sf_singleNews">
....
    <p>
       <PDF:Attachment ID="PDFAttachment" runat="server" />
    </p>
....
</div>

Hope this little trick helped you, let me know.

Labels: ,


Comments:
Why don't you use <%#%> and call the databind() method for the hyerlink....
 
That would cause Sitefinity to stop editing the page due to <%%> in collection error.

Also if you follow my blog I suggest you check out my new home on the web zubairahmed.net (Know that I have to put a message here). See you there.
 
Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?