Search code examples
c#asp.netcss

Adding CSS at runtime to an ASP.net master page.


I have an ASP.net web application, and would like to know if is it possible to change the CSS of a site at run time of a master page, and get the CSS file name from a SQL database and add it into the system ?


Solution

  • You can put on the top of the masterpage on aspnet literal and construct the link in the pageload of the masterpage that way you could put a css there

    <asp:Literal ID="Css" runat="Server" />
    

    Then on the page load

    var cssfilename  = GetCssFromDatabase();
    Literal.Text = "<link rel=\"stylesheet\" media=\"all\" " + cssfilename  + "\" />";