从特定div

时间:2016-05-08 05:21:24

标签: mysql css asp.net vb.net dynamic-css

我有一个div,其属性设置为runat =“server”及其ID。我正在尝试将来自MySQL数据库的特定div的动态背景图像放在行中提到的路径url中。现在一切正常,但我不知道如何只给那个特定的div带来风格影响。目前我只在div上定义了结果,以获得该页面上所有div的背景图像。如何在代码中定义div类或ID?

Private Sub coverContent_Init(sender As Object, e As EventArgs) Handles coverContent.Init
        Try
            Dim css As New HtmlGenericControl()
            css.TagName = "style"
            css.Attributes.Add("type", "text/css")
            Dim imageURL As String = String.Empty
            Dim var3 As String
            var3 = Request.QueryString("hospitalID")

            Dim str As String = "Select hospitalID, coverImage from hospitals where hospitalID='" + var3 + "';"
            con.Open()
            Dim cmd As New MySqlCommand(str, con)
            Dim da As New MySqlDataAdapter(cmd)
            Dim dt As New DataTable
            da.Fill(dt)
            con.Close()
            If dt.Rows.Count > 0 Then
                generalID.Text = dt.Rows(0)("hospitalID").ToString
                imageURL = dt.Rows(0)("coverImage").ToString
            End If
            con.Close()
            css.InnerHtml = (Convert.ToString("div{background-image: url(") & imageURL) + ");}"

            Page.Header.Controls.Add(css)

            MyBase.OnInit(e)
        Catch ex As Exception
            Response.Write(ex)
        End Try
    End Sub

1 个答案:

答案 0 :(得分:0)

在你提到div的地方非常简单,你需要指定你想要显示背景图像的div类。如下所示

css.InnerHtml = (Convert.ToString(".divClassName{background-image: url(") & imageURL) + ");}"