Sitefinity ASP.NET CMS - Content Management System

KB Article

Home >  Support >  Knowledge Base >  KB Article
How to display filename instead of full path when selecting items from Images and Documents module - ID#1042
Rating: Not rated
Last Modified: 7/10/2008
Related categories: Modules;

Article information

Article relates to

Sitefinity 3.x 

Created by

 Penka Ivanova

Last modified by

 Rebecca


Problem
When selecting an item from Images and Documents module using a button selector, the full path to the file gets in the textbox and you have to put the cursor towards the end in order to see the actual filename. Isn't there a way to display the filename only?

Solution
To display the filename instead of the full URL in the textbox associated with the button selector, you can wrap the ButtonSelector and the TextBox in a user control and implement the required functionality in it. Note that you need to keep the real value in a hidden field.

Here is an example:
.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ButtonSelectorWrapper.ascx.cs" Inherits="UserControls_Custom_Controls_ButtonSelectorWrapper" %> 
<%@ Register TagPrefix="sfLib" Namespace="Telerik.Libraries.WebControls" Assembly="Telerik.Libraries" %> 
<script type="text/javascript"
 
function beforeInsertLink(src, altText, title, width, height)  
    if (src != '') 
    { 
        var urlHidden = document.getElementById('<%= selectedValueField.ClientID %>'); 
        if (urlHidden) 
            urlHidden.value = src
    } 
 
    if (title != '') 
    { 
        var urlTxt = document.getElementById('<%= urlTextBox.ClientID %>'); 
                 
        if (urlTxt) 
            urlTxt.value = title
    } 
</script> 
 
<asp:TextBox ID="urlTextBox" runat="server"></asp:TextBox> 
<sfLib:ButtonSelector ID="ButtonSelector1" AssociatedControls="selectedValueField" runat="server" /> 
<asp:HiddenField runat="server" ID="selectedValueField" /> 

.ascx.cs
public partial class UserControls_Custom_Controls_ButtonSelectorWrapper : System.Web.UI.UserControl, ITextControl 
    protected void Page_Load(object sender, EventArgs e) 
    { 
 
    } 
    #region ITextControl Members 
 
    public string Text 
    { 
        get 
        { 
            return this.selectedValueField.Value; 
        } 
        set 
        { 
            int lastIndex = value.LastIndexOf('/'); 
            if (lastIndex != -1) 
            { 
                string fileName = value.Substring(lastIndex + 1); 
                if (fileName.EndsWith(".sflb")) 
                    this.urlTextBox.Text = HttpUtility.UrlDecode(fileName.Replace(".sflb""")); 
            } 
 
            this.selectedValueField.Value = value; 
        } 
    } 
    #endregion 

The attached ButtonSelectorWrapper control implements the ITextControl interface, so there is no problem to use the above approach in the Sitefinity administration, in a ControlPanelInsert. ascx module template, for example.

Article Files

  • ButtonSelectorWrapper.zip



  • Article Comments

    Seth Cleaver, 8/12/2008
    HI, That's great how about displaying the thumbnail of the selected image instead of the filename? IS that a similar process? Thanks Seth

    Telerik Admin, 8/20/2008
    Seth, It is a bit more complicate than the example here. If you need it urgently, could you please open a support or forum thread?


    Please Sign In to rate this article or to add it to your favorites.