【问题标题】:ASP.Net How to programmatically add a background image for my site?ASP.Net 如何以编程方式为我的网站添加背景图片?
【发布时间】:2016-02-02 23:15:53
【问题描述】:

我想以编程方式向我的网站添加背景图片,但是我遇到的代码不起作用。

首先我使用 Javascript 打印浏览器尺寸:

$(document).ready(function () {
    $("#clientScreenWidth").val($(window).width());
    $("#clientScreenHeight").val($(window).height());
});

之后,我通过隐藏输入将这些维度发送到 vb.net 并为其分配值:

Dim height As String = HttpContext.Current.Request.Params("clientScreenHeight")
Dim width As String = HttpContext.Current.Request.Params("clientScreenWidth")

但是,当我尝试将这些参数输入到字符串中并将其传递给我身体的 css 时,出现了错误:

Dim BackgroundImageURL As String = "https://unsplash.it/" + height + "/" + width
body.Style.Add("background-image: url(""" + BackgroundImageURL + """);")

重载解析失败,因为没有可访问的“添加”接受此数量的参数。

编辑:

发帖说我修复了过载解决错误!

语法是"classid.style.add("first part of css name", "second part")"

示例:body.style.add("background-image", "url(""http://google.com/logo.png"")")

【问题讨论】:

  • 查看 Style.Add 的文档,虽然我不确定你为什么不全部使用 JavaScript/jQuery。
  • 谢谢。我会用 JS 来做,很难相信我忽略了这么简单的一步!

标签: javascript html css asp.net vb.net


【解决方案1】:

要在 .NET 代码隐藏中编辑正文样式,请添加一个 ID 和 runat="server"。然后在 .NET 中引用它变得更容易。

<body id="body1" runat="server">

</body>

后面的代码:

Dim BackgroundImageURL As String = "https://unsplash.it/" + height + "/" + width

body1.Attributes["style"] = "background-image: url(\"" + BackgroundImageURL + "\");"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    • 1970-01-01
    • 2022-12-04
    相关资源
    最近更新 更多