1

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.

JPJedi
  • 1,498
  • 7
  • 32
  • 58
  • Are you looking for a client-side solution (javascript), or do you want to perform a complete postback to the server? – Daniel Szabo Dec 31 '11 at 16:43
  • U can try with java scripts, or you saving the background color after selecting from the dropdown, check this http://stackoverflow.com/questions/5068087/set-background-colour-of-select-to-selected-option-in-jquery – Thillai Narayanan Dec 31 '11 at 16:48

4 Answers4

2

Did you search at all?

PageBody.Attributes("bgcolor") = "green"

Make sure you set you body element to runat ="Server"

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

Taken from here

atoMerz
  • 7,534
  • 16
  • 61
  • 101
0

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 = ...

Robert Beaubien
  • 3,126
  • 4
  • 22
  • 29
  • I have the selection and the color value to change the color to, It is I just can't access the page(Website page) backround color in VB.Net to actually change the color to that. Thats the code I am looking for. – JPJedi Dec 31 '11 at 16:40
  • Sorry, I missed the web part of that. Was thinking winforms. Moiz has the answer for you. – Robert Beaubien Dec 31 '11 at 16:44
0

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>
Mo3z
  • 2,138
  • 7
  • 21
  • 29
  • 1
    please note that `document.bgColor` is [deprecated](https://developer.mozilla.org/en/DOM/document.bgColor). Instead, use `document.body.style.backgroundColor` – naveen Dec 31 '11 at 16:54
0

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.

Working demo here.

<!-- 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.

Daniel Szabo
  • 7,181
  • 6
  • 48
  • 65