Formulaire de changement de couleur dans ASP.NET

Hé les gars, je suis en train d'écrire un simple programme web pour obtenir mes pieds mouillés dans C#.Net et ASP.Net et je suis un peu confus.

fondamentalement, ce que je veux faire est d'avoir une liste déroulante dans laquelle l'utilisateur peut choisir la couleur qu'ils veulent l'arrière-plan de la page, mais je ne trouve pas la propriété de le faire de manière dynamique comme ça.

pas que cela importe, mais je suis en utilisant visual studio 2010.

Des idées?

S'il vous plaît et merci!

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>
&nbsp;&nbsp;&nbsp;
<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>
  • MVC ou WebForms?
  • WebForms, je pense, lol. Ce n'est pas MVC.
InformationsquelleAutor Mike | 2012-03-25