Stupid Romeo Stupid Romeo - 2 years ago 73
SQL Question

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
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 + "';"
Dim cmd As New MySqlCommand(str, con)
Dim da As New MySqlDataAdapter(cmd)
Dim dt As New DataTable
If dt.Rows.Count > 0 Then
generalID.Text = dt.Rows(0)("hospitalID").ToString
imageURL = dt.Rows(0)("coverImage").ToString
End If
css.InnerHtml = (Convert.ToString("div{background-image: url(") & imageURL) + ");}"


Catch ex As Exception
End Try
End Sub

Answer Source

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) + ");}"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download