首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ASP.NET中更改窗体颜色

问在ASP.NET中更改窗体颜色
EN

Stack Overflow用户
提问于 2012-03-26 04:06:08
回答 4查看 21.7K关注 0票数 0

嘿,伙计们,我正在写一个简单的web程序来尝试C#.Net和ASP.Net,我有点困惑。

基本上,我想做的是有一个下拉框,用户可以在其中选择他们想要的页面背景颜色,但我找不到属性来动态地这样做。

这并不重要,但我使用的是visual studio 2010。

有什么想法吗?

请和谢谢!

代码语言:javascript
复制
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;
    }
}

来源:

代码语言:javascript
复制
<%@ 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>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-03-26 04:11:29

代码语言:javascript
复制
<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> 

代码隐藏:

代码语言:javascript
复制
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; 
        } 
    } 
票数 3
EN

Stack Overflow用户

发布于 2012-03-26 04:20:38

你对网站、浏览器以及你看到的渲染页面是如何工作的有一个根本性的误解。

它们的工作方式与Windows窗体不同,在Windows窗体中,您可以为各种内容设置属性。相反,您需要了解HTML是如何工作的,以及CSS (HTML使用的样式语言)。如果您不了解浏览器如何呈现页面,您将永远无法了解页面上的内容如何显示。

一旦你了解了如何设置网页的背景颜色,你就可以知道如何使用asp.net来实现同样的功能。但现在,您假设它的工作方式与Winforms相同。事实并非如此。

如果没有对超文本标记语言和层叠样式表工作原理的基本理解,你就永远不能理解ASP.NET是如何工作的。我建议,无论何时尝试做某事,首先要弄清楚它是如何在标准HTML中完成的。

票数 3
EN

Stack Overflow用户

发布于 2012-03-26 04:16:10

您必须更改样式属性,而不是属性属性。

Style property

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9863554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档