前言
在使用DNN进行模块开发时会经常用到一些DNN内置支持的服务端用户控件。这次本文将主要介绍的就是如何在模块开发过程中使用UrlControl,以及介绍一下大家普遍遇到的问题。希望本文可以为那些对DNN感兴趣的童鞋有帮助。
使用场景
通过UrlControl我们可以在界面中收集用户选择/上传的网站内部文件、输入的外部链接、选择的用户信息连接。我最近使用UrlControl的场景就是:在一个百度地图展现模块的设置页面中为模块指定一个百度地图上的标注点图片以达到标注点图标的定制化效果。
使用方式
请参见DNN Wiki页面对使用方式的如下解释
1、注册用户控件
To use the control within a module control there is a two step process, first register the control
<%@ Register TagPrefix="dnn" TagName="UrlControl" Src="~/controls/UrlControl.ascx"%>
2、在用户控件中放置UrlControl2、在用户控件中放置UrlControl
Then place the control where desired
<dnn:UrlControl ID="ControlNameHere" runat="server"/>
不过其中对于属性的说明确不尽人意(继承了DNN文档的一如既往的不明确、不完整风格,还是本意是鼓励大家多看源码?)
Property | Description/Note |
ShowFiles | IF true, allow users to select files, if false they will not be able to select/upload files. |
ShowLog | Allow users to see tracking statistics for clicks to the resources within the URL Control, downloads, clicks throughs, etc. |
ShowTrack | Allow for the tracking of clicks and downloads of the resources accessed via the Url Control, only then accessible through the ShowLog property |
ShowUpload | Allow users to upload files through this instance of the Url Control. File upload permissions are dependant upon the File permissions via the Admin - File Manager page. |
ShowNewWindow | If true, when creating a link to a resource (file/url) you can allow for that link to open in a new window) |
UrlType | Acceptable values, F, U, P, for File (link to a file), U (link to a URL) or P (link to an existing DNN page) |
Url | If the user has provided a URL you can populate the setting through this property. |
Log | Boolean value to see if the user has selected the log option |
Track | Boolean value to see if the user has selected the Track option |
NewWindow | Boolean value to see if the user has selected the New Window option |
在这还是贴一个国内网站列举的比较全的属性说明,方便今后写代码时参考。
FileFilter | 文件类型。可以选择和上传得类型,多个类型用逗号分隔。如:jpg,gif,txt |
LocalResourceFile | 该控件对应的语言资源文件。这个不用设置,默认就可以了。 |
Log | 获取和设置该链接是否记录点击日志。及记录详细的访问人访问时间等。 |
ModuleID | 设置链接属于的模块。这个不需要专门的设置。这个控件会默认获取当前所在模块的ID。 |
NewWindow | 获取和设置该链接是否通过新窗口打开。 |
Required | 获取和设置该链接是否必须填写。默认:True |
ShowDatabase | 是否显示保存在数据库中的文件。默认:True |
ShowFiles | 是否显示添加文件选择。默认:True |
ShowLog | 是否显示记录日志选择。默认:True |
ShowNewWindow | 是否显示新窗口打开链接选择。默认:False |
ShowNone | 是否显示无。默认:False。 |
ShowSecure | 是否显示保存在文件系统中的文件。默认:True |
ShowTabs | 是否显示网站内部页面选择。默认:True |
ShowTrack | 是否显示设置记录链接点击次数选择。默认:True |
ShowUpLoad | 是否显示文件上传。默认:True |
ShowUrls | 是否显示默认:True |
ShowUsers | 是否显示添加一个查看用户信息的链接。默认:False |
Track | 获取和设置是否记录链接的点击次数。 |
Url | 获取和设置链接值 |
UrlType | 获取和设置当前链接类型。N:无;U:外部链接;T:内部链接;F:文件链接;M:用户信息链接 |
Width | 控件宽度 |
常见问题
写代码不碰到问题是不可能的,在使用UrlControl的时候我就碰到了如下问题。就此跟大家分享一下心得:
1、通过UrlControl选中文件链接后,得到的url的格式为fileid=123。 这明显和我想要得到的图片链接沾不上边啊。其实DNN已经帮我们实现好了转换方式,只是文档上没有说明而已。以下是我先前使用的一部分代码片段,仅供参考。
var rawFileUrl = Convert.ToString(TabModuleSettings[ModuleSettingNames.IconUrl]);
if(rawFileUrl != null && rawFileUrl.Length > 7){
var fileID = rawFileUrl.Substring(7);
var fi = DotNetNuke.Services.FileSystem.FileManager.Instance.GetFile(int.Parse(fileID));
var url =PortalSettings.HomeDirectory + fi.RelativePath;
}
2、那通过UrlControl.Url得到的属性值存储到数据中后,有怎么判断到底是什么链接类型呢。简单,看一下下面的代码就了解了。
_Urltype = Globals.GetURLType(_Url).ToString("g").Substring(0, 1);
其中Globals.GetURLType返回的是一个枚举值,其类型为TabType。截取枚举值的首字母,我们就可以得到先前属性说明中提到的几个值定义。
3、既然可以通过UrlControl获得链接信息了,那DotNetNuke.Common.Globals.LinkClick又有什么作用呢
其实翻看源码便可以知道,先上代码:
/// <summary>
/// Gets Link click url.
/// </summary>
/// <param name="Link">The link.</param>
/// <param name="TabID">The tab ID.</param>
/// <param name="ModuleID">The module ID.</param>
/// <param name="TrackClicks">if set to <c>true</c> [track clicks].</param>
/// <param name="ForceDownload">if set to <c>true</c> [force download].</param>
/// <param name="PortalId">The portal id.</param>
/// <param name="EnableUrlLanguage">if set to <c>true</c> [enable URL language].</param>
/// <param name="portalGuid">The portal GUID.</param>
/// <returns>Formatted url.</returns>
public static string LinkClick(string Link, int TabID, int ModuleID, bool TrackClicks, bool ForceDownload, int PortalId, bool EnableUrlLanguage, string portalGuid)
{
string strLink = "";
TabType UrlType = GetURLType(Link);
if (UrlType == TabType.Member)
{
strLink = UserProfileURL(Convert.ToInt32(UrlUtils.GetParameterValue(Link)));
}
else if (TrackClicks || ForceDownload || UrlType == TabType.File)
{
//format LinkClick wrapper
if (Link.ToLowerInvariant().StartsWith("fileid="))
{
strLink = ApplicationPath + "/LinkClick.aspx?fileticket=" + UrlUtils.EncryptParameter(UrlUtils.GetParameterValue(Link));
}
if (String.IsNullOrEmpty(strLink))
{
strLink = ApplicationPath + "/LinkClick.aspx?link=" + HttpUtility.UrlEncode(Link);
}
// tabid is required to identify the portal where the click originated
if (TabID != Null.NullInteger)
{
strLink += "&tabid=" + TabID;
}
// moduleid is used to identify the module where the url is stored
if (ModuleID != -1)
{
strLink += "&mid=" + ModuleID;
}
//only add language to url if more than one locale is enabled, and if admin did not turn it off
if (LocaleController.Instance.GetLocales(PortalId).Count > 1 && EnableUrlLanguage)
{
strLink += "&language=" + Thread.CurrentThread.CurrentCulture.Name;
}
//force a download dialog
if (ForceDownload)
{
strLink += "&forcedownload=true";
}
}
else
{
switch (UrlType)
{
case TabType.Tab:
strLink = NavigateURL(int.Parse(Link));
break;
default:
strLink = Link;
break;
}
}
return strLink;
}
从以上代码便可以知道,通过LinkClick函数我们可以将类似fileid=123的url传给它,然后它会根据参数生成在页面中可单击的链接。但是对于如果是引用站内的图片的话还是要使用常见问题一中的方式得到正确的图片url。
4、上传同名文件直接覆盖的问题
有国人已经遇到过该问题并且通过修改开源代码的方式为UrlControl添加了一个属性使其在上传时碰到同名文件不覆盖已有文件(请参看附录1)。
不过我还是认为既然要通过写代码的方式来达到这个目的,还是新建一个项目通过继承DNN UrlControl的方式来实现,这样即使是DNN升级后也不妨碍你继续使用你这个定制后的功能。
5、在代码中给UrlControl.Url赋值后在页面渲染后不做任何操作UrlControl.Url值返回空字符窜的问题
该问题困扰了我一阵,也没有找到所以然来。空了再看看代码,如果有了解其中原理的童鞋请不吝赐教。
参考(附录)
1、http://www.cnblogs.com/ajaxleoxu/archive/2010/04/07/1706816.html 通过修改DNN UrlControl源文件使解决上传同名文件直接覆盖的问题
2、http://www.dnnsoftware.com/wiki/Page/dnnurlcontrol 官网UrlControl Wiki Page
3、http://techkb.it168.com/Articles%5C9%5C7%5Ca%5C97a498f05871ee1154fbb1c5d44fba12.htm 博文-DNN开发细解2