Search code examples
mysqlcssasp.netvb.netdynamic-css

changing css background from sql database of particular div


I have a div where it's property set runat="server" with its ID. I'm trying to put dynamic background image for that particular div from MySQL database where it's path url mentioned in row. Now all working fine but I don't know how to give style affect to that particular div only. Currently I defined only on div which results to get background image in all div on that page. How can I define that div class or ID in my code?

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

Solution

  • Very simple where you have mentioned div there you need to specify class of div where you want to show background image. Like below

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