I have a drop downList and I have it listing colors. When a color is selected I want to change the background color of the page itself.
I am using Visual Studio 2008 and using VB.Net.
I have a drop downList and I have it listing colors. When a color is selected I want to change the background color of the page itself.
I am using Visual Studio 2008 and using VB.Net.
Well, here is code that changes the background color of the combo box itself on changing of the selection:
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
With ComboBox1
Select Case .Text
Case "red"
.BackColor = Color.Red
Case "green"
.BackColor = Color.Green
Case "blue"
.BackColor = Color.Blue
Case Else
.BackColor = Nothing
End Select
End With
End Sub
If you mean to change the background color of the whole winform, use me.BackColor = ...
Use javascript:
<select onChange="changeBackground()" id="myselect"><option></option><option value='black'>black</option></select>
<script type="text/javascript">
function changeBackground(){
var select = document.getElementById("myselect");
var color = select.options[select.selectedIndex].value;
document.bgColor = color;
}
</script>
Not sure why you'd want to do a full postback to the server just so that you can change the background color of the page, but here's the quick and easy client-side javascript solution.
<!-- HTML -->
<select id="sample">
<option value="white">White</option>
<option value="red">Red</option>
<option value="yellow">Yellow</option>
<option value="blue">Blue</option>
</select>
// JavsScript
<script>
document.getElementById("sample").onchange = function(){
document.body.style.background = this.options[this.selectedIndex].value;
}
</script>
Or, you can follow your original line of thought and do it the server-side way...but that seems like a lot more work. I suppose it makes sense if you need to store the background color in a server-side variable for other uses (theming, personal prefs, etc)... but client-side cookies tend to be a better place for that kind of thing.