Hey guys I’m writing a simple web program to get my feet wet in C#.Net and ASP.Net and I’m a little confused.
basically what I want to do is have a drop down box where the user can select the color they want the background of the page to be, but I can’t find the property to do it dynamically like that.
not that it matters but I’m using visual studio 2010.
Any ideas?
Please and thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void cmdSubmit_Click(object sender, EventArgs e)
{
if (txtName.Text == "")
{
}
else
{
lblName.Visible = true;
lblName.Text = "Well hello there " + txtName.Text + "!";
lblColor.Visible = true;
ddlColors.Visible = true;
}
}
protected void ddlColors_SelectedIndexChanged(object sender, EventArgs e)
{
int strDdlValue = Convert.ToInt32(ddlColors.SelectedValue);
switch (strDdlValue)
{
case 1:
Body.Style["background-color"] = "Red";
break;
case 2:
Body.Attributes["bgcolor"] = "blue";
break;
case 3:
Body.Attributes["bgcolor"] = "magenta";
break;
case 4:
Body.Attributes["bgcolor"] = "green";
break;
default:
break;
}
lblBye.Visible = true;
}
}
SOURCE:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body id="Body" bgcolor="#3366ff" runat="server">
<form id="form1" runat="server" visible="True">
<div align="center" style="font-size: medium; font-weight: bold" >
<asp:Label ID="lblWelcome" runat="server" Text="Welcome to WebGreeting!"></asp:Label>
<br />
<br />
<br />
<asp:Label ID="lblInstruction1" runat="server"
Text="Please enter your name in the text box below:"></asp:Label>
<br />
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="cmdSubmit" runat="server" onclick="cmdSubmit_Click" Text="Submit!" />
<br />
<br />
<br />
<asp:Label ID="lblName" runat="server"></asp:Label>
<br />
<br />
<br />
<asp:Label ID="lblColor" runat="server" Text="What's your favorite color?"
Visible="False"></asp:Label>
<br />
<br />
<br />
<asp:DropDownList ID="ddlColors" runat="server"
onselectedindexchanged="ddlColors_SelectedIndexChanged" Visible="False">
<asp:ListItem></asp:ListItem>
<asp:ListItem>Red</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
<asp:ListItem>Blue</asp:ListItem>
<asp:ListItem>Yellow</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<br />
<asp:Label ID="lblBye" runat="server" Text="I hope you had a nice day!"
Visible="False"></asp:Label>
<br />
</div>
</form>
</body>
</html>
Code Behind: