1

I have 3 css files (File.css,File2.css,File3.css) with same class name "BkImg" which changes the background image of the page and depending upon some condition in my codebehind page I want to link one of these file. In my aspx body tag ().

I am using C# as code behind language

0

2 Answers 2

2

Similar to this question.

An adaptation of an answer on that question..

You can use the Page_Init function in your code behind file to dynamically generate the link and add it to your page header (or body, in your case). An example of that function is below in C#. You would, of course, implement your logic to change the Href value.

protected void Page_Init(object sender, EventArgs e)
{
    var link = new HtmlLink();
    link.Href = "~/styles/main.css";
    link.Attributes.Add("rel", "stylesheet");
    link.Attributes.Add("type", "text/css");
    Page.Body.Controls.Add(link);
}

Make sure that you put a runat="server" in the body tag so that you can reference the body from the code behind file.

<body runat="server">
</body>
Sign up to request clarification or add additional context in comments.

Comments

1

First add 'id' and 'runat' property for body tag.

<body id="mybody" runat="server">

then you can add your calss dynamically with page_load event or another event.

protected void Page_Load(object sender, EventArgs e)
{

   mybody.Attributes.Add("class", "classname();");

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.