Mike
Mike

Reputation: 129

Change form color in ASP.NET

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>
&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>

Upvotes: 0

Views: 23933

Answers (4)

user1276958
user1276958

Reputation:

   <asp:DropDownList ID="ddlColors" runat="server" OnSelectedIndexChanged="ddlColors_SelectedIndexChanged"
        Visible="False" AutoPostBack="true">
        <asp:ListItem Value="0"></asp:ListItem>
        <asp:ListItem Value="1">Red</asp:ListItem>
        <asp:ListItem Value="2">Green</asp:ListItem>
        <asp:ListItem Value="3">Blue</asp:ListItem>
        <asp:ListItem Value="4">Yellow</asp:ListItem>
    </asp:DropDownList>

add the value attributes like this. this code is working i tried now. no problem occur

Upvotes: 0

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93434

You have a fundamental misunderstanding of how web sites, and browsers and thus the rendered pages you see work.

They do not work like Windows Forms, where you set properties for various things. Instead, you need to understand how HTML works, and CSS (the style language used by HTML). If you don't understand how a browser renders a page, you will forever be lost in understanding how things show up on the page.

Once you understand how to set the background color of a web page, then you can figure out how to use asp.net to achieve the same functionality. But right now, you are assuming it works the same way as Winforms. It doesn't.

Without a fundamental understanding of how HTML and CSS work, you will never be able to understand how ASP.NET works. I suggest that whenever you try to do something, you first figure out how it's done in standard HTML.

Upvotes: 3

JotaBe
JotaBe

Reputation: 39015

You have to change the Style property, not the Attributes property.

Style property

Upvotes: 0

user1276958
user1276958

Reputation:

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Untitled Page</title> 
</head> 
<body id="Body" runat="server"> 
<form id="form1" runat="server"> 
<div> 
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged"> 
        <asp:ListItem>1</asp:ListItem> 
        <asp:ListItem>2</asp:ListItem> 
        <asp:ListItem>3</asp:ListItem> 
        <asp:ListItem>4</asp:ListItem> 
    </asp:DropDownList> 
</div> 
</form> 
</body> 
</html> 

Code Behind:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)

    { 
    int strDdlValue = Convert.ToInt32(DropDownList1.SelectedValue); 
    switch (strDdlValue) 
        { 
        case 1: 
            Body.Attributes["bgcolor"] = "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; 
        } 
    } 

Upvotes: 3

Related Questions