在ASP.NET的母版页中使用图片和超链接

在母版页中使用相对地址的时候要特别小心.特别是在母版页中添加图片和超链接的时候。HTML标记或ASP.NET控件中,相对地址的解析是不一样的。

如果你在ASP.NET控件中使用相对地址,相对地址被解析为相对母版页的地址,例如,假设你在母版页中添加了一个图片:

<asp:Image ImageUrl="Picture.gif" Runat="Server" />

如果母版页在一个MasterPages文件夹中,这个地址会被解析为:

/MasterPages/Picture.gif

如果引用母版的内容页在另一个不同的文件夹中,图片地址还是会被解析为这个相对于母版页的地址。

在HTML标记中使用相对地址则不同,这个图片地址会被解析为相对于引用母版页的内容页的地址。

解决的方法有三个:

第一种方法,把HTML标记替换成ASP.NET标记。

第二种方法,使用绝对地址,而不是相对地址,例如,在名为MyApp的应用程序中,可以使用以下<img>标签来显示在MasterPages文件夹中的图片:

<img src="/MyApp/MasterPages/Picture.gif" />

第三种方法,在母版页中使用方法来重新解析相对地址,如程序清单5.10所示。

我的解决办法:

1、把母版页和引用页都放在项目根目录下,想把母版页放在主题文件夹下的,系统不让。

2、把母版用的图片放在主题文件夹下,和CSS文件在一起。

01.<%@ Master Language="VB" %> 
02.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
03."http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
04. 
10. 
11. 
14. 
15.

16.

17. 18. 19. 20. 21. 22.

23. 24. 25.

You must be careful when using relative URLs in a Master Page. For example, you must be careful when adding images and links to a Master Page. Relative URLs are interpreted in different ways, depending on whether they are used with HTML tags or ASP.NET controls.

If you use a relative URL with an ASP.NET control, then the URL is interpreted relative to the Master Page. For example, suppose that you add the following ASP.NET Image control to a Master Page:

<asp:Image ImageUrl="Picture.gif" Runat="Server" />

The ImageUrl property contains a relative URL. If the Master Page is located in a folder namedMasterPages, then the URL is interpreted like this:

/MasterPages/Picture.gif

Even if a content page is located in a completely different folder, theImageUrl is interpreted relative to the folder that contains the Master Page and not relative to the content page.

The situation is completely different in the case of HTML elements. If an HTML element such as an<img> or<a> tag includes a relative URL, the relative URL is interpreted relative to the content page. For example, suppose you add the following<img> tag to a Master Page:

<img src="Picture.gif" />

The src attribute contains a relative URL. This URL is interpreted relative to a particular content page. For example, if you request a content page located in a folder namedContentPages, the relative URL is interpreted like this:

/ContentPages/Picture.gif

Using relative URLs with HTML elements is especially tricky because the URL keeps changing with each content page. If you request content pages from different folders, the relative URL changes. There are three ways that you can solve this problem.

First, you can replace all the HTML elements that use relative URLs with ASP.NET controls. An ASP.NET control automatically reinterprets a relative URL as relative to the Master Page.

Note

Relative URLs used by ASP.NET controls in a Master Page are automatically reinterpreted relative to the Master Page. This process of reinterpretation is calledrebasing. Only ASP.NET control properties decorated with theUrlProperty attribute are rebased.

Second, you can avoid relative URLs and use absolute URLs. For example, if your application is named MyApp, then you can use the following<img> tag to display an image file located in theMasterPages folder:

<img src="/MyApp/MasterPages/Picture.gif" />

The disadvantage of using absolute URLs is that they make it difficult to change the location of a web application. If the name of your application changes, then the absolute URLs will no longer work and you'll end up with a bunch of broken images and links.

Another option is to use a method to reinterpret relative URLs in a Master Page. For example, the Master Page inListing 5.10 includes a method namedMasterUrl(). This method is used with the<img> tag in the body of the Master Page, which displays the website logo.

Listing 5.10. MasterPages\ImageMaster.master
<%@ Master Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">Public Function MasterUrl(ByVal url As String) As StringReturn String.Format("{0}/{1}", Me.TemplateSourceDirectory, url)End Function
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server"><title>Image Master</title>
</head>
<body><form id="form1" runat="server"><div><img src='<%=MasterUrl("Logo.gif") %>' alt="Website Logo" /><asp:contentplaceholder id="ContentPlaceHolder1" runat="server" /></div></form>
</body>
</html>

The Master Page in Listing 5.10 is located in a folder namedMasterPages. This folder also includes an image namedLogo.gif. This image is displayed with the following HTML tag:

<img src='<%=MasterUrl("Logo.gif") %>' alt="Website Logo" />

The MasterUrl() method appends the image's filename to the value of the Master Page'sTemplateSourceDirectory property. TheTemplateSourceDirectory property represents the folder that contains the Master Page.

The content page inListing 5.11 uses the Master Page and correctly displays the website logo (seeFigure 5.5):

Figure 5.5. Displaying a Master Page relative image.
[View full size image]

Listing 5.11. ImageContent.aspx

<%@ Page Language="VB" MasterPageFile="~/MasterPages/ImageMaster.master" %><asp:ContentID="Content1"ContentPlaceHolderID="ContentPlaceHolder1"Runat="Server"><h1>Content</h1></asp:Content>

转载于:https://www.cnblogs.com/WestGarden/archive/2011/11/23/3138386.html

在ASP.NET的母版页中使用图片和超链接,HTML标记和ASP.NET标记的不同相关推荐

  1. asp自动解析网页中的图片地址,并将其保存到本地服务器

    程序实现功能:自动将远程页面的文件中的图片下载到本地. 程序代码 <% '将本文保存为 save2local.asp '测试:save2local.asp?url=http://ent.sina ...

  2. Asp.Net在SqlServer中的图片存取

    在使用asp.net将图片上传并存入SqlServer中,然后从SqlServer中读取并显示出来  一,上传并存入sqlserver   数据库结构    create table test     ...

  3. HTML5——如何在网页中加入图片和超链接。

    声明:此页内容极度简单,觉得low的话我可以不看 寻找图片,把图片的位置记好,并且重命名,方便寻找. 写出基本框架,引用< img >标签. 找出你需要跳转的网页,记好网址使用< a ...

  4. 母板页中的图片路径及页面链接路径设置

    昨天写好一个应用程序,在localhost跑都OK的,发布到IIS后,别人一访问图片不显示,超链接的指向都找不到页面, F12一看,路径都有问题,不会吧,我写的都是相对地址啊. 母版页中的图片我用的都 ...

  5. 测试ASP.NET 2.0中Gridview控件高级技巧

    ASP.NET 2.0中,新增加的gridview控件的确十分强大,弥补了在asp.net 1.1中,使用datagrid控件时的不足之处.因为在asp.net 1.1中,在使用datagrid时,很 ...

  6. ASP.NET 2.0中使用Gridview控件的高级技巧

    ASP.NET 2.0中,新增加的gridview控件的确十分强大,弥补了在asp.net 1.1中,使用datagrid控件时的不足之处.因为在asp.net 1.1中,在使用datagrid时,很 ...

  7. 如何在ASP.Net 中把图片存入数据库

    介绍 可能有很多的时候,我们急需把图片存入到数据库当中.在一些应用程序中,我们可能有一些敏感的资料,由于存储在文件系统(file system)中的东西,将很容易被某些用户盗取,所以这些数据不能存放在 ...

  8. ASP.NET中的图片路径问题

    ASP.NET中的图片路径问题,相信大家都遇到过,而且很烦.比如,我们的图片路径是在根目录\images\下,在主页面我们想用里面的图片,在用户控件中我们也想用那个目录下的图片,而用户控件往往我们会把 ...

  9. asp.net 2中的图片上传

    BETA 2出来了,暂时还没得到,因此还是用BETA 1研究.在asp.net 2中,如何实现图片的上传展示呢?下面,我用GRIDVIEW搭配文件上传功能进行实现(而实现文件上传的原理大致一样).在A ...

  10. [ASP.NET2.0] asp.net在ie7中使用FileUpload上传前预览图片 [ZT]

    asp.net在ie7中使用FileUpload上传前预览图片 因为安全性问题,IE7禁用了image控件引用本地图片,为了这个问题郁闷了好几天,终于找到了解决方案,好东西要与大家分享,代码如下: 此 ...

最新文章

  1. 【Android 性能优化】应用启动优化 ( 方法追踪代码模板 | 示例项目 | SD 卡访问权限 | 示例代码 | 获取 Trace 文件 | Android Studio 查看文件)
  2. 隔离公司各个部门--虚拟路由器(RIP)
  3. Python+Opencv常用小工具集合
  4. 2017.7.28 愤怒的小鸟 思考记录
  5. idea抽取重复方法快捷键_IDEA 真牛逼,900行 quot;又臭又长quot; 的类重构,几分钟搞定...
  6. 二分法02:寻找第一个和最后一个的满足条件的位置
  7. 求交集和并集的线性算法
  8. 惠普p1106打印机安装步骤_惠普p1106打印机驱动程序下载
  9. 【Pix4d精品教程】Pix4Dmapper完整航测内业操作流程手把手图文教程
  10. 降噪耳机简介及降噪技术-ANC、ENC、DSP、CVC
  11. SQL 筛选某一时间大于某一个值的数据及数量
  12. RK3399平台开发系列讲解(电源管理篇)11.10、PMIC(生产者)驱动数据结构体
  13. python jQuery
  14. WinDbg手动修复堆栈
  15. 利用GPS定位[android]
  16. 【优化求解】基于自适应模拟退火粒子群优化算法求解单目标优化问题matlab代码
  17. 用python生成个性二维码_python生成个性二维码学习笔记
  18. linux的翻译系统开发,Linux下类似金山词霸的翻译软件
  19. DB2的日期时间类型以及转换问题
  20. 回溯算法解决智能拼图的最小步骤的问题

热门文章

  1. C 线性表的链式存储实现及插入、删除等操作示例
  2. webpack打包初体验
  3. 阿里云服务器疑似误报异地登录?怎么查看
  4. JavaScript编码风格指南(中文版)
  5. leetcode-337-打家劫舍三*
  6. HISI3536安装交叉编译工具链
  7. 数据库中主键与索引的区别
  8. intellij idea 程序包不可见问题
  9. 使用Struts2和jQuery EasyUI实现简单CRUD系统(五)——jsp,json,EasyUI的结合
  10. iis 访问网站需要进行身份验证