Hi,

I seek your help as m stuck with an issue. I am uploading a css style file in a folder and need to provide the link to that css file in my aspx page. That is simple, but the twist is, the file name can be anything. That file is stored in a folder and needs to be applied in such a way, that be it any name, the css file should be fetched and styling be applied from that css file in my project.

Please help.

Simple... Access the Folder and get the FileName.
Check the extension of Filee. If it is CSS
then apply it to the Page as Page.Style dynamically

Thank You Bhuvan.. I will try that and get back to you !! :)

I'm not familiar with the Page.Style approach, but if you want once you have the file name that you read from the disk, you can create the link element dynamically in the head section of your document. Here are two examples. The first is typically used for creating the link element. The second can be used for the link element, but is generally used for other types of elements in the head section.

    Dim myCSS As New HtmlLink
    myCSS.Href = "style.css"
    myCSS.Attributes.Add("rel", "stylesheet")
    Page.Header.Controls.Add(myCSS)

The first example is typically used for creating the link element. The second example below can be used for creating the link element, but it does not create a singleton (<link />) as in the first example. This technique will create an opening and closing tag (<link><link />). So while you can still use it for the link element, it would be more appropriate say for creating a <style> element where you can add content between the tags.

    Dim myCSS As New HtmlGenericControl
    myCSS.TagName = "link"
    myCSS.Attributes.Add("rel", "stylesheet")
    myCSS.Attributes.Add("href", "style.css")
    Page.Header.Controls.Add(myCSS)

The code shown above is written in VB, but you can easily modify it for C#.

Hi Jorge,

Thank You so much for your help. I will try this out also. But the css I wish to apply, is actually in a folder in my project. The folder will be uploaded by the user and can have any name. Taking that into consideration, I wish to provide a link such that be it any name of the css file, that file should be linked and the css be applied from that file, to my project. Is it possible ?

Hi,

Just one correction. The file will be uploaded by any user and it will be at the location of the specified folder. Also, the folder will or may contain various css so I will have to search the file starting from a unique ID say something like "C20" and locate the file, which will have to mentioned in the link, and it is this file whose css is to be applied, to my project. Is it possible ? I am also trying my best from my end. But your help would help me do it better. Please Guide !!

So in the first example I provided, line 2, replace that string with the information you get from the user input. Give that a try.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.