Hi ilanocf
In the PageLoad event you need to retrieve the css from database and write to the Header of the page using the style tag.
Please refer below sample.
I have refered below article for inserting data in database.
Save (Insert) and Retrieve (Display) TinyMCE contents from database in ASP.Net
HTML
<asp:TextBox ID="txtHTMLContent" runat="server" TextMode="MultiLine" />
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="Submit" />
<hr />
<table>
<tr>
<th>Customer Id</th>
<th>Name</th>
<th>Country</th>
</tr>
<tr>
<td>1</td>
<td>John Hammond</td>
<td>United States</td>
</tr>
<tr>
<td>2</td>
<td>Mudassar Khan</td>
<td>India</td>
</tr>
<tr>
<td>3</td>
<td>Suzanne Mathews</td>
<td> France </td>
</tr>
<tr>
<td>4</td>
<td>Robert Schidner</td>
<td>Russia</td>
</tr>
</table>
<br />
<div class="container">
<div class="row">
<div class="col-md-4">
<img src="https://www.aspsnippets.com/images/Logo_new.png" style="background-color: #0090CB;" /></div>
<div class="col-md-8">
Welcome to ASPSnippets.</div>
</div>
</div>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.0.20/tinymce.min.js"></script>
<script type="text/javascript">
tinymce.init({ selector: 'textarea', width: 500 });
</script>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
AddCSS();
}
}
private void AddCSS()
{
string css = string.Empty;
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT [Content] FROM [HTMLContent]", con))
{
con.Open();
css = Convert.ToString(cmd.ExecuteScalar());
con.Close();
}
}
HtmlGenericControl style = new HtmlGenericControl();
style.TagName = "style";
style.Attributes.Add("type", "text/css");
style.InnerHtml = css;
this.Page.Header.Controls.Add(style);
}
protected void Submit(object sender, EventArgs e)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO [HTMLContent] VALUES (@Content)", con))
{
cmd.Parameters.AddWithValue("@Content", txtHTMLContent.Text.Replace("<p>", string.Empty).Replace("</p>", string.Empty).Replace(" ", string.Empty));
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
AddCSS()
End If
End Sub
Private Sub AddCSS()
Dim css As String = String.Empty
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT [Content] FROM [HTMLContent]", con)
con.Open()
css = Convert.ToString(cmd.ExecuteScalar())
con.Close()
End Using
End Using
Dim style As HtmlGenericControl = New HtmlGenericControl()
style.TagName = "style"
style.Attributes.Add("type", "text/css")
style.InnerHtml = css
Me.Page.Header.Controls.Add(style)
End Sub
Protected Sub Submit(ByVal sender As Object, ByVal e As EventArgs)
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("INSERT INTO [HTMLContent] VALUES (@Content)", con)
cmd.Parameters.AddWithValue("@Content", txtHTMLContent.Text.Replace("<p>", String.Empty).Replace("</p>", String.Empty).Replace(" ", String.Empty))
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Response.Redirect(Request.Url.AbsoluteUri)
End Sub
Screenshot
Web Page with applied CSS from database
Inserted CSS in database