简体   繁体   English

W3C验证包含CSS的UserControl

[英]W3C Validate UserControl which contains CSS

I have written a .net usercontrol for a gallery which works quite well in my web application if there is a gallery on my page. 我为画廊编写了一个.net用户控件,如果页面上有画廊,该控件在我的Web应用程序中将非常有效。 The problem is when I go to validate my page, using XHTML 1.0 Strict, I get the document type does not allow element "link" here. 问题是,当我使用XHTML 1.0 Strict验证网页时,在此文档类型不允许元素“链接”。

I've done a bit of research and it is because I have a stylesheet embedded within the usercontrol, and is called from the body of the masterpage, so it doesn't validate. 我做了一些研究,这是因为我在用户控件中嵌入了一个样式表,并且该样式表是从母版页的正文中调用的,因此它无法验证。 I can move the stylesheet out of the usercontrol and into the masterfile, but I don't necessary want the stylesheet loading up on every single page. 我可以将样式表移出用户控件,然后移入主文件中,但是我并不需要在每个页面上都加载样式表。 What is the best way to do this - I was hoping to contain the stylesheet within the usercontrol so its simple to 'manage'. 做到这一点的最佳方法是什么-我希望将样式表包含在usercontrol中,以使其易于“管理”。

Any advice would be great. 任何建议都很好。 Also thinking, do I need to remove the javascript out of the usercontrol as well? 还在想,我是否也需要从用户控件中删除JavaScript?

This is the user control in question 这是有问题的用户控件

<%@ Control Language="C#" Debug="true" AutoEventWireup="true" CodeFile="Gallery.ascx.cs" Inherits="Gallery" %>

<%@ Register Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI.WebControls" TagPrefix="asp" %>

<link ID="FancyboxCSS" type="text/css" rel="stylesheet" href="" runat="server" />
<link ID="FancyboxButtonsCSS" type="text/css" rel="stylesheet" href="" runat="server" />

<script type="text/javascript">

$(document).ready(function () {
    $(".fancybox").fancybox({
        maxWidth: 800,
        maxHeight: 800,
        fitToView: true,
        aspectRatio: true,
        openEffect: 'fade',
        closeEffect: 'fade',
        nextEffect: 'fade',
        prevEffect: 'fade',

        helpers: {
            title: {
                type: 'outside'
            },
            overlay: {
                opacity: 0.8,
                css: {
                    'background-color': '#000'
                }
            },
            buttons: {}
        }
    });
});

<div id="gallery" runat="server">
//images here
</div>

A markup validator processes documents in a markup language, such as HTML or XML, and a document must be complete. 标记验证器使用标记语言(例如HTML或XML)处理文档,并且文档必须完整。 For example, an HTML document must minimally contain a title element. 例如,HTML文档必须至少包含一个title元素。 And things like <%@ should not be included: they are not part of the HTML document but instructions to software that generates HTML. 而且不应该包括<%@类的东西:它们不是HTML文档的一部分,而是有关生成HTML的软件的说明。

So you need to submit a complete, generated HTML document, if you wish to validate. 因此,如果要验证,您需要提交完整的,生成的HTML文档。

Found the answer here for anyone else looking for the same thing in the future http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmllink(v=vs.80).aspx 在这里为其他将来寻找相同事物的其他人找到了答案http://msdn.microsoft.com/zh-cn/library/system.web.ui.htmlcontrols.htmllink(v=vs.80).aspx

// Define an HtmlLink control.
HtmlLink myHtmlLink = new HtmlLink();
myHtmlLink.Href = "~/StyleSheet.css";
myHtmlLink.Attributes.Add("rel", "stylesheet");
myHtmlLink.Attributes.Add("type", "text/css");

// Add the HtmlLink to the Head section of the page.
Page.Header.Controls.Add(myHtmlLink);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM