Ok, so at the end of the day, the goal here matters.
Say I want to color some "City" or "project name". Or maybe even some project status.
Then it makes sense to write a bit of code for such a goal.
So, say I want a custom color for "citys" in my system.
Say, I build this grid:
So, above is this simple grid:
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/spectrum-colorpicker2/dist/spectrum.min.css" />
<script src="https://cdn.jsdelivr.net/npm/spectrum-colorpicker2/dist/spectrum.min.js"></script>
<asp:GridView ID="GridColors" runat="server"
AutoGenerateColumns="False"
DataKeyNames="ID"
CssClass="table table-hover"
width ="30%"
>
<Columns>
<asp:TemplateField HeaderText="City">
<ItemTemplate>
<asp:TextBox ID="txtCity" runat="server"
Text='<%# Eval("City") %>'
></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Set Color">
<ItemTemplate>
<asp:TextBox ID="txtColor" runat="server"
Text='<%# Eval("Color") %>'
>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="cmdAdd" runat="server" Text="Add"
CssClass="btn"
OnClick="cmdAdd_Click"
/>
<asp:Button ID="cmdSave" runat="server" Text="Save"
CssClass="btn" style="margin-left:30px"
OnClick="cmdSave_Click"
/>
<!-- Initialize Spectrum on the input field -->
<script>
$('[id*=txtColor]').spectrum({
type: 'component', // Choose the mode (e.g., component, color, text)
showInput: true, // Show the input field
showInitial: true, // Show the initial color
allowEmpty: false, // Allow empty selection
showAlpha: false, // Enable alpha transparency
disabled: false, // Set to true to disable
// Other options can be customized as needed
});
</script>
And the code behind is also rather simple:
Dim rstData As New DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadGrid()
Else
rstData = Session("rstData")
End If
End Sub
Sub LoadGrid()
rstData = MyRst("SELECT * FROM tblCityColor")
Session("rstData") = rstData
GridColors.DataSource = rstData
GridColors.DataBind()
End Sub
Protected Sub cmdSave_Click(sender As Object, e As EventArgs)
For Each gRow As GridViewRow In GridColors.Rows
With rstData.Rows(gRow.RowIndex)
Dim ColorBox As TextBox = gRow.FindControl("txtColor")
.Item("Color") = ColorBox.Text
Dim txtCity As TextBox = gRow.FindControl("txtCity")
.Item("City") = txtCity.Text
End With
Next
SaveData(rstData, rstData.TableName)
' display the grid of hotels
Response.Redirect("~/Grids/Hotels.aspx")
End Sub
Now, in the page that shows the Grid of hotels?
Well, I simple left join in our color table, and thus how have a "color" for each city.
This:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadData() ' first time grid load
End If
If RBColor.SelectedIndex = 1 Then
AddCss("~/Content/DarkMode.css")
End If
End Sub
Sub LoadData()
Dim strSQL As String =
"SELECT *, Color FROM tblHotelsA
LEFT JOIN tblCityColor on tblCityColor.City = tblHotelsA.City"
GVHotels.DataSource = MyRst(strSQL)
GVHotels.DataBind()
End Sub
So, now on row data bind, I can set the background color of the city cell.
this code:
Protected Sub GVHotels_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim gData As DataRowView = e.Row.DataItem
If Not IsDBNull(gData("Color")) Then
e.Row.Cells(2).Style.Add("background-color", gData("Color"))
End If
End If
End Sub
However, while the above approach is fine for say setting a color for a given value?
If you goal is to "theme" the page, and change many controls?
Then you want to use a style sheet. They are "amazing" and are designed for this.
Note in above, I have a radio button choice called dark mode.
No need to write a WHOLE bunch of code to change all the controls and their colors.
Just a SUPER simple sheet, set background color, grid line color (tr td elemements) and that of a button.
The style sheet is this:
body {
background-color: black;
color: skyblue;
border: 1px solid green;
}
button {
background-color: lightskyblue;
}
input {
background-color: lightskyblue;
border-radius:15px;
padding:5px;
}
tr td {
border-color: skyblue;
}
How simple the above is. So, now, when I click on the dark mode button, everything changes like this:
So, for a theme, or having to change lots of controls? We don't write code for that anymore, and we have css style sheets. So, it really depends on your goal here. If you wanting to change a LOT of controls and have some "theme" choice? Then of course we don't write that code, but will setup some styles for the page.
However, for a "individual" setting like some city, project status, or whatever? Then yes, it makes sense to save such data in a database, and use such color values when you render such data to the page.
I can post a bit more code. So, much of the trick here is finding a color picker that you like, and the REAL issue is there are too many to choose from!