C# 使用母版时在网页上设置背景图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1343143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 15:23:08  来源:igfitidea点击:

Set background picture on webpage when using a masterpage

c#asp.nethtmlmaster-pages

提问by CruelIO

Is there away to set a background image on a webpage when the webpage is based on a masterpage. Notice its not the background of the masterpage i want to change, but the background of the page which use the masterpage.

当网页基于母版页时,是否可以在网页上设置背景图像。请注意,它不是我要更改的母版页的背景,而是使用母版页的页面的背景。

采纳答案by goldenratio

You can just add a style rule for body{} or whatever on the actual ASPX page. It will override any style set at the master page level.

您可以为 body{} 或实际 ASPX 页面上的任何内容添加样式规则。它将覆盖在母版页级别设置的任何样式。

edit: example:

编辑: 示例:

<%@ Page Title="Example" Language="C#" MasterPageFile="~/MasterPages/Main.master" 
    AutoEventWireup="true" CodeBehind="TroubleShootScanning.aspx.cs" 
    Inherits="SpectrumTechnologies.TroubleShootingScanning" %>

<asp:Content ID="Content1" ContentPlaceHolderID="mainContent" runat="server">
    <style type='text/css'>
        body { background-image: url(images/bgimage.jpg); }
    </style>
    <!-- the rest of the page here -->
</asp:Content>

This will override any values set in a stylesheet.

这将覆盖样式表中设置的任何值。

回答by moribvndvs

You could put a content placeholder on the master page that is within the header.

您可以在页眉内的母版页上放置一个内容占位符。

Then in your content page, put a content control where you could include the second modified CSS stylesheet or STYLE block directly.

然后在您的内容页面中,放置一个内容控件,您可以在其中直接包含第二个修改后的 CSS 样式表或 STYLE 块。

回答by msftwise

There's an additional trick to add to this...(Second line below) Also helps the background show while testing locally.

还有一个额外的技巧可以添加到这个......(下面的第二行)也有助于在本地测试时显示背景。

回答by Colin

I suggest adding an <asp:ContentPlaceHolder ID="ExtraStyles" runat="server" />tag to the header of the Masterpage. That way you can add the following to your page:

我建议<asp:ContentPlaceHolder ID="ExtraStyles" runat="server" />在 Masterpage 的标题中添加一个标签。这样您就可以将以下内容添加到您的页面中:

<asp:Content ID="ExtraStylesContent" ContentPLaceHolderId="ExtraStyles" runat="server">
  <style type="text/css">
  body {background-image:url('someotherimage.jpg');
  </style>
</asp:Content>

By adding an extra ContentPlaceHolder you won't get the scattered style tags, they will end up in the head tag of the rendered html.

通过添加额外的 ContentPlaceHolder,您将不会获得分散的样式标签,它们最终会出现在呈现的 html 的 head 标签中。