User Control, href, SRC, and CSS Path Problems

Source: Internet
Author: User

<% @ Control Language = "C #" autoeventwireup = "true" codebehind = "header. ascx. cs" inherits = "darwinportal. usercontrols. Header" %>
<Link href = "<% = resolveurl ("~ /CSS/global.css ") %>" rel = "stylesheet" type = "text/CSS"/>
<A class = "logo" href = '<% = resolveurl ("~ /Default. aspx ") %> 'title =" dextrys home "> dextrys home </a>

Head. ascx:
<Link href = "CSS/css.css" type = "text/CSS" rel = "stylesheet">
Call head. ascx
<TD background = "images/bar_bg.gif"> <script language = "JavaScript" src = "JS/fade. js"> </SCRIPT>
<Table>
</Table>
</TD>
<Asp: Image runat = "server" id = "image1" imageurl = "../images/developer.gif"> </ASP: Image>

<TD background = "images/background .gif"> // you cannot solve this path problem.

Question 1:

Now we find that the image and IMG are written differently when specifying the image path.
<Asp: Image imageurl = added above ../

Why.

Let's analyze it.
Index. aspx When HTML is finally generated. Head. ascx is actually loaded to the root directory, so the head. ascx and images directories are of the same level.

IMG labels use HTML directly.Code. So it does not add ../when the image is specified ../

<Asp: image is a server control. If you do not add ../images to it, after the final page is generated. Will generate
Http: // localhost/INF/usercontrol/images/developer.gif
Instead
Http: // localhost/INF/images .gif. And <asp: The image is intelligent. It can dynamically adjust the final HTML generated based on the location of the loaded page. Ensure that the image path is good.

Finally, let's give it a try. We found that http: // localhost/INF/index. all images in aspx are normal, while http: // localhost/INF/Module 1/others. in aspx, <asp: image is displayed normally, while

Problem 1 solution:
A: All images use the Server Control <asp: image. However, there is no way for those images as the <TD cell background
B: or do not divide the directories. All aspx files are thrown to the root directory. (Completely solved. All problems are not problems. But it is the most spam solution, the wholeProgramDirectory structure is really bad)

Question 2:
Currently, image problems are basically solved. But I forgot it. There is also a CSS and JS script in head. ascx. Of course, to solve this problem, add the correct path on the page that calls head. ascx.

But now
<TD background = "images/bar_bg.gif"> <script language = "JavaScript" src = "JS/fade. js"> </SCRIPT>
<Table>
</Table>
</TD>
This section.
This JS script is special. It only serves on the table before <TD> </TD>. If it is added throughout the page. Then the scope of the function will change.

Obtain the DNS host name or IP Address:
Httpcontext. Current. Request. url. Host

Obtain the root path of the virtual program:
Httpcontext. Current. Request. applicationpath

These two parts are the names of your virtual sites.

<Asp: Image id = "image1" runat = "server" imageurl = "~ /Images/head. jpg "/>

(1) No problems will occur when using server controls. The server path can be automatically converted on the server side. If you add resolveurl, you will find that the conversion is superfluous!

2) if an HTML control is used, the condition is: if there is a user control in a user control directory, this user control uses the HTML control to display images in other directories, if the image cannot be correctly displayed when the user control is dragged to the webpage in the root directory, you must use the resolveurl method to avoid any problems. Statement: '/>
'alt = "test "/

(3) If you want to help with the data in the database in HTML code, such as a link, you can write it:
<A href = '<% # resolveurl ("~ /Default. aspx? Param = ") + eval ('dataitem') %> '> return </a>
If you write code in the background, such as a link, you can write it as lable1.text = "<a href =" + this. resolveurl (".../view. aspx? Id = ")" + ID + "> View </a>"
(I also encountered this problem when writing background code, so I wrote this article.Article)

Conclusion: If you use an HTML control or HTML Tag, you 'd better add the resolveurl method to prevent errors. If you use a server control, you do not need to consider the resolveurl method.

 

. Ascx is just a control. It must be carried by aspx, that is, the connections generated by. ascx are subject to the aspx that carries the ascx!

. aspx indicates that you want to connect to. aspx, so it is normal to not find it! If the control folder is. aspx and B. aspxc. if the three pages of aspx are in the root directory of the site, there will be no errors. If they are in the virtual directory or actual directory of the site, there will be errors! That is to say, http://www.cnblogs.com/the east and west of this type are only valid for the directory, and the site is invalid!
if you think you are not sure about the path, we recommend that you use ASP: hyperlink so that you can use ~ /To indicate the root path ~~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.