ResolveUrl和Me.TemplateSourceDirectory是什么意思!

ninesong 2003-09-02 01:08:58
item.ImageUrl = ResolveUrl(Me.TemplateSourceDirectory & "\tree.jpg")
其中item是treeNode。偶就是不明白
ResolveUrl(Me.TemplateSourceDirectory & "\tree.jpg")
是什么意思,手头的MSDN又坏了。

给分,!给分!!!坚决给分!!!!!!
...全文
191 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
PoorAngel 2003-09-02
  • 打赏
  • 举报
回复
将 URL 转换为在请求客户端可用的 URL。

[Visual Basic]
Public Function ResolveUrl( _
ByVal relativeUrl As String _
) As String

[C#]
public string ResolveUrl(
string relativeUrl
);

[C++]
public: String* ResolveUrl(
String* relativeUrl
);

[JScript]
public function ResolveUrl(
relativeUrl : String
) : String;

参数
relativeUrl
与 TemplateSourceDirectory 属性相关联的 URL。
返回值
转换后的 URL。

异常
异常类型 条件
ArgumentNullException 当 relativeUrl 参数包含空引用(Visual Basic 中为 Nothing)时发生。

备注
如果 relativeUrl 参数包含绝对 URL,则该 URL 原样返回。如果 relativeUrl 参数包含相对 URL,则该 URL 将更改为与当前请求路径相符的相对 URL,这样浏览器便能够解析该 URL。

例如,请考虑以下方案:

客户端已请求了一个 ASP.NET 页,该页含有一个用户控件,该用户控件有一个关联的图像。
ASP.NET 页位于 /Store/page1.aspx。
用户控件位于 /Store/UserControls/UC1.ascx。
图像文件位于 /UserControls/Images/MyPhoto1.jpg。
如果用户控件将图像的相对路径(即 /Store/UserControls/Images/MyPhoto1.jpg)传递给 ResolveUrl 方法,则该方法将返回值 /UserControls/Images/MyPhoto1.jpg。

此方法使用 TemplateSourceDirectory 属性解析为绝对 URL。返回的 URL 适用于客户端。

示例
[Visual Basic, C#, C++] 下面的示例创建一个 Image Web 服务器控件对象,并使用 ResolveUrl 方法设置该图像的路径(该路径由 ImageUrl 属性存储)。

[Visual Basic]
' Use the Context property to write text to the Response object
' associated with the current request.
Context.Response.Write("<br><br>ParentControl's OnBubbleEvent called.")
Context.Response.Write(("<br>Source of event is: " + sender.ToString()))

[C#]
// Use the Context property to write text to the Response object
// associated with the current request.
Context.Response.Write("<br><br>ParentControl's OnBubbleEvent called.");
Context.Response.Write("<br>Source of event is: " + sender.ToString());

[C++]
// Use the Context property to write text to the Response Object*
// associated with the current request.
Context->Response->Write(S"<br><br>ParentControl's OnBubbleEvent called.");
Context->Response->Write(String::Concat(S"<br>Source of event is: ", sender));

[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 。

要求
平台: Windows 2000, Windows XP Professional, Windows Server 2003 系列
PoorAngel 2003-09-02
  • 打赏
  • 举报
回复
明白了吗?
PoorAngel 2003-09-02
  • 打赏
  • 举报
回复
控件和适配器可以调用移动 Web 控件的 ResolveUrl 方法解析出 URL,此 URL 相对于包含此控件的页面或用户控件的目录。

public string ResolveUrl(
string relativeUrl
)
参数
relativeUrl
相对于包含此控件的页面或用户控件的目录的 URL。
备注
适配器基类中的一些帮助器方法自动调用 ResolveUrl 方法来解析超级链接。
场景:在自定义控件、用户控件、页面、后台代码都会有引用JS的可能,这就会出现混乱或者重复引用的可能。 一个自定义控件,用于在ASPX页面中注册JS: public class Script : Control {   #region 属性   private string m_Src;   ///    /// 脚本文件路径   ///    public string Src   {     get { return m_Src; }     set { m_Src = value; }   }   #endregion   ///    /// 在控件Init的时候将JS路径添加到HttpContext.Current.Items["IncludedJavaScript"]中。   ///    ///    protected override void OnInit(EventArgs e)   {     base.OnInit(e);     if (!string.IsNullOrEmpty(Src))     {       string src = ResolveUrl(Src);       List includedJs = HttpContext.Current.Items["IncludedJavaScript"] as List;       if (null == includedJs)       {         includedJs = new List();         HttpContext.Current.Items["IncludedJavaScript"] = includedJs;       }       if (!includedJs.Contains(src))       {         includedJs.Add(src);       }     }   } } 一个静态类,用于管理JS和在后台代码(cs文件)中注册JS: ///  /// Javascript管理器 ///  public static class JavaScriptManager {   ///    /// 包含JS引用。   ///    ///    public static void Include(params string[] filePaths)   {     HttpContext context = HttpContext.Current;     if (null == context)     {       throw new Exception("HttpContext为空。");     }     System.Web.UI.Page p = context.CurrentHandler as System.Web.UI.Page;     if (null == p)     {       throw new Exception("HttpContext.CurrentHandler不是Page。");     }     IList jss = GetIncludedJavaScript();     string resolveUrl;     foreach (string filePath in filePaths)     {       resolveUrl=p.ResolveUrl(filePath);       if (!jss.Contains(resolveUrl))       {         jss.Add(p.ResolveUrl(resolveUrl));       }     }   }   ///    /// 获取已经包含的JS列表   ///    ///    public static IList GetIncludedJavaScript()   {     HttpContext context = HttpContext.Current;     if (null == context)     {       throw new Exception("HttpContext为空。");     }     IList jss = HttpContext.Current.Items["IncludedJavaScript"] as IList;     if (null == jss)     {       jss = new List();       HttpContext.Current.Items["IncludedJavaScript"] = jss;     }     return jss;   } } 然后写一个基类页面,所有的页面都要继承自这个基类页: public class BasePage : System.Web.UI.Page {   public BasePage() { }   #region 注册/管理JS引用   ///    /// 将引用的JS添加到Page.Head中。   ///    private void InitJS()   {     IList includedJs = JavaScriptManager.JavaScriptManager.GetIncludedJavaScript();     foreach (string jsFilePath in includedJs)     {       var script = new HtmlGenericControl("script");       script.Attributes["type"] = "text/javascript";       script.Attributes["src"] = jsFilePath;       Page.Header.Controls.Add(script);     }   }   ///    /// 在呈现之前注册JS   ///    ///    protected override void OnPreRender(EventArgs e)   {     base.OnPreRender(e);     InitJS();   }   #endregion } 上面是在OnPreRender中将JS注册到Page.Head中的,所以如果在自定义控件中注册JS引用,请在OnPreRender之前引用。 在ASPX页面中注册JS:                                  在CS页面中注册JS: public partial class _Default : BasePage {   protected void Page_Load(object sender, EventArgs e)   {     JavaScriptManager.JavaScriptManager.Include("~/JS/cs.js",       "~/JS/cs.js",       "~/JS/cs.js2",       "~/JS/cs.js");   } }

62,039

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧