My method is to convert all the controls into server controls, that is, add runt = "server", and then at home "~ . In this way, it always means that there is no problem with development and release under the root directory of the website.
Below are some methods for reprinting
Currently, the Sichuan Mobile project has encountered a small problem: how to write the relative path of the script referenced on the master page?
Cause: whether the relative path is for the master page or for the content page. The answer is the content page (the master page only replaces the content page). In this way, a problem occurs: when the content page and the master page are not in the same directory, when the content pages are placed in different folders, different content pages have different relative paths for script reference.
Question: If JavaScript and CSS are referenced in the masterpage of the master page, this problem does not exist if all the pages applying the template are in the same path. However, in practice, some folders are often created for management purposes, and the referenced masterpage is put under different folders. This is because of Path Problems in the JS file referenced by masterpage, you may not be able to access the content pages on the apply master page. How can this problem be solved?
Solution: through your own efforts and online references, you can find some solutions:
Method 1:
String jsfile = "<script language = \" javascript \ "src = \" {0} \ "type = \" text/JavaScript \ "> </SCRIPT> ";
Response. Write (string. Format (jsfile, resolveurl ("~ /JavaScript/jquery. js ")));
This method can solve the problem quickly, but response. write writes the reference of the script to the beginning of the HTML page, which breaks down the XHTML format and brings various risks. According to data: Developed with Asp.net Ajax, it is found that many controls in Asp.net ajaxcontroltoolkit do not work. Later it was found that the result was caused by response. Write.
Method 2:
String jsfile = "<script language = \" javascript \ "src = \" {0} \ "type = \" text/JavaScript \ "> </SCRIPT> ";
Page. header. innerhtml = string. Format (jsfile, resolveurl ("~ /JavaScript/jquery. js "));
This method can be implemented and can solve problems in a good way.
Method 3:
Htmllink link = new htmllink ();
Link. href = string. Format (jsfile, resolveurl ("~ /CSS/base.css "));
Link. attributes ["rel"] = "stylesheet"; Link. attributes ["type"] = "text/CSS ";
Page. header. Controls. Add (Link );
This is a method for registering styles.
Method 4: the method of registering client scripts is implemented in combination with the scriptmanager control in ASP. NET Ajax.
Use scriptmanager. This is a control of Asp.net ajaxconroltoolkit. With this control, you can dynamically and dynamically follow the script path of the referenced master page, automatically converts the paths referenced in the master page from the master page to the relative paths relative to the referenced content page. The usage is as follows:
<Asp: scriptmanager id = "scriptmanagermain" enablepagemethods = "true" runat = "server">
<Scripts>
<Asp: scriptreference scriptmode = "Auto" Path = "~ /JavaScript/common. js "/> here is the relative path relative to the master page, of course, you can also use Path = "~ /JS/jscript1.js "(relative to the root directory of the website) can also be implemented.
</Scripts>
</ASP: scriptmanager>
Prerequisites: Ajax is installed in. <asp: scriptmanager> is only available for components in the. NET environment. Note that the control also needs to specify a resource script file for the program, which is in the system. web. in extensions. Therefore, you must specify in Web. config:
<Httphandlers>
<Add verb = "Get, head" Path = "scriptresource. axd "type =" system. web. handlers. scriptresourcehandler, system. web. extensions, version = 1.0.61025.0, culture = neutral, publickeytoken = 31bf3856ad364e35 "Validate =" false "/>
</Httphandlers>
Other methods:
1. since the script paths referenced by different pages are different, separate them, instead of placing the referenced script files on the master page, and place the reference of the script files on the referenced pages respectively. This is not a solution. Although it is not a good solution, it can solve this problem.
2. Use the root directory of the server where your website is located. Note that it is not the root directory of your website. It is represented by the symbol "/", but this is less portable. Therefore, an error occurs when the root directory of the website is different from the root directory of the server.
Last, I would like to add some symbols:
./Image/and image/relative path
../Relative to the upper-level path of the current path
~ /Is the relative path relative to the root directory of the website. (Note that this can only be identified on the server, but not on the client)
/Indicates the relative path relative to the root directory of the server where the website is located (note that this is relative to the root directory of the server rather than the root directory of the website)