Updating the background of a specific div element by retrieving data from an SQL database

I have a unique situation where I have a div element with the property set to runat="server" and an ID assigned to it. I am attempting to dynamically set the background image for this specific div from a MySQL database where the path URL is stored in a row. Everything is functioning properly, however, I am struggling with applying styling effect to only that particular div. Currently, I have defined the style on the div itself, which results in all divs on the page having the same background image. How can I assign a class or ID to this div in my code to target it specifically?

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#uniqueDiv{background-image: url(") & imageURL) + ");}"

        Page.Header.Controls.Add(css)

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

Answer №1

If you're looking to add a background image to a specific div, make sure to include the class of that div in your CSS code. Here's an example:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to retrieve chosen "CheckBoxes" from a tree view with JavaScript?

I am currently working with the asp TreeView control and I am interested in obtaining the selected values of "checkboxes" on the client side. Is there a way for me to retrieve the selected values from the TreeView? ...

How to Customize Background Colors for Blogspot Posts?

Recently, I started my own blog using Google Blogger and I've run into a minor issue. Despite applying a background image for the entire blog, all of my posts have a white background instead. I haven't used any background-color in the HTML code o ...

"Enable a smooth transition and switch the visibility of a div element when clicked

How to create a dropdown form with a transition effect that triggers on click of an element? Once triggered, the transition works smoothly but clicking again only hides the div element without triggering the transition. See the demo here: Check out the fu ...

Transmit JSON data containing a base64 encoded string of an imageview

Looking for help to send an ImageView Android image to a server URL and store it in a server database (PhpMyAdmin). Inserting a simple string works fine, but when dealing with an ImageView, issues arise. Here is the code: public class AddnewActivity ext ...

Troubleshooting MODULE NOT FOUND Error in my First NodeJS Application: A guide to resolving common issues

Welcome to My NodeJS Learning Project! Embarking on my first NodeJS application journey, this project serves as a stepping stone for me to grasp the fundamentals. The goal is to create a basic CRUD web app for item management. Drawing from my experience i ...

Only conceal the table column if space is limited

A scenario involves an HTML table with two columns that need to be displayed fully, with the second column aligned to the right. Currently, this has been achieved by setting the width of the first column to 100%. However, a challenge arises where the words ...

Learn how to create an animated refreshing icon in React when clicked

I have a refresh icon in my React app that utilizes Material UI. I want the icon to spin for a second after it is clicked, but I am unsure of how to achieve this. Despite attempting some solutions, none have been successful. const useStyles = makeStyles(( ...

Explore for data within the fields that are included in different records

My database table has a massive 200,000 rows with a city column that looks something like this: rowid city 1 Orlando 2 Miami 3 Key West 4 San Francisco 5 Los Angeles 6 Las Vegas I am trying to identify all r ...

A guide to transforming rows into columns with CSS

Hello, I am trying to convert rows into columns using CSS. Currently, my code looks like this: <style> .flex-container { max-width: 500px; width: 100%; display: flex; flex-wrap: wrap; } .flex-item { ...

What is the best way to alter the background of a div when hovering over an image?

I am looking to achieve a unique effect with my thumbnail image and background. Currently, I have the thumbnail image inside a div with another image serving as the background to frame it. My goal is to change the position of the background image from bott ...

Transform one div to another using a CSS animation slide

I am experiencing an issue with two divs (page1 + page2) inside a container (also a div). The container has overflow:hidden property, but when the animation starts, the overflow configuration is being ignored. Additionally, the page that should be displaye ...

Tips for creating a responsive Youtube embedded video

Check out this website for a good example: If you take a look, you'll notice that the header youtube video is responsive - it automatically resizes when the window size changes. Here are the <iframe> codes: <iframe id="bluetube-player-1" fr ...

Set meanmenu to a fixed position

I am currently utilizing the meanmenu plugin to create a responsive menu for mobile devices. My goal is to fix the position of the menu at the bottom of the page so that users can easily access it. However, when I set the position of the meancontainer to ...

In order to activate the button, make sure all 3 boxes are checked

Can someone provide some guidance on the script below? I'm utilizing it for a pop-up when a user attempts to add a product to the cart. Presently, the button will activate if at least one checkbox is selected (although it's not working on fiddle ...

Sorting the main Laravel Eloquent model using a related model by Jensenggers

I am seeking assistance in sorting the entire dataset of the main model through a column of a relational model. My stack consists of Laravel ORM 5.2.43 and Jensenggers MongoDb 3.1 Below are the models I'm working with: UserEventActivity.php - MongoD ...

Adjust the color of the label when the checkbox is marked, and make it red when the checkbox is left

Looking to create a customized checkbox due to challenges in styling the default HTML checkbox, I have attempted the following code. The checkbox functions properly when clicking on the label, but I am unable to change the border color of the label based o ...

Configuring static files in Django for hosting a website in production mode

I have been attempting to serve my static files from the same production site in a different way. Here are the steps I took: First, I updated the settings.py file with the following: DEBUG = False ALLOWED_HOSTS = ['12.10.100.11', 'localhos ...

Guide to positioning an image at the center across 2 columns using Bootstrap

Looking to create a split screen Bootstrap 4 layout with an image in the center overlapping the two columns. Have successfully set up two equal columns but struggling to position the image to overlap. Have experimented with position:absolute and float:lef ...

``In Internet Explorer, the function to swap the image source when hovering over the parent element

I'm trying to make it so that when I hover over the parent (Li) tag with my mouse, the src attribute of the child img tag changes. This code works correctly in Chrome but not in Firefox and Internet Explorer. <li class="player"> . . //some c ...

The Conflict-Free Dilemma of JQuery Cycle

Everything was working smoothly with the implementation of the JQuery Cycle Plugin. However, as I attempted to link a separate JavaScript file for a Menu in the Head Section from this source , <script type="text/javascript" src="js/ddmegamenu.js"> ...