日期:2014-05-18  浏览次数:20432 次

ASP。net中如何改变HTML控件背景色和BODY背景色
初学ASP。net写了如下成程序,希望改变Wed背景色和上的的文本框(HTML控件和WEB控件)背景色,结果发现WED控件可以改变,
但文档和HTML控件的背景出错,请指教。附源码如下:(VS2003开发)
webform1.aspx.vb
Imports System.Drawing.Color



Public Class WebForm1
  Inherits System.Web.UI.Page
   
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Text1.BackColor = Red
  Lbl.BackColor = Wheat
  txt2.bgcolor = "#ff0000"
  mybody.BgColor="#ff0000"
  End Sub
End Class

-----------------webform1.aspx-----------------------------------
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="WebtestColor.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body id="mybody" runat="server">
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 224px; POSITION: absolute; TOP: 176px" runat="server"
Text="Button"></asp:Button>
<asp:TextBox id="Text1" style="Z-INDEX: 102; LEFT: 208px; POSITION: absolute; TOP: 104px" runat="server"
Height="56px" BackColor="#C0C0FF"></asp:TextBox>
<asp:Label id="Lbl" style="Z-INDEX: 103; LEFT: 208px; POSITION: absolute; TOP: 64px" runat="server"
BackColor="#80FF80" Width="136px">Label</asp:Label>
<INPUT type="text" id="txt2" name="txt2" runat="server">
</form>
</body>
</HTML>


------解决方案--------------------
Input控件没有bgcolor这个属性 使用style控制。
------解决方案--------------------
txt2.Style["background-color"] = "#ff0000" ;
mybody.Style["background-color"]="#ff0000" ;
------解决方案--------------------
button1.Attributes.Add("onclick","javascript:body.bgcolor=#####");
------解决方案--------------------
用Css控制..
------解决方案--------------------
在html中加入runat="server"一样在后台调用
------解决方案--------------------
(Page.FindControl("mybody") as System.Web.UI.HtmlControls.HtmlGenericControl).Style["background-color"]="#ff0000" ;