ASP.NET中直接用C# 动态修改CSS样式
2017-02-09 15:10
435 查看
ASP.NET中直接用C#
动态修改CSS样式
wonsoft
(wonsoft@163.com)
使用JavaScript控制CSS样式有点麻烦,还是觉得直接使用C#操作更方便快捷,本文通过两个Button控制TextBox1的高度和背景色,以展示通过C#控制CSS样式的方法。以下是操作的实例:
一、前端代码(TestEditStyle.aspx.)如下:
<%@ Page Language="C#"AutoEventWireup="true" CodeFile="TestEditStyle.aspx.cs"Inherits="Test_TestEditStyle" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//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>
<formid="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server"Text="Button" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server"Text="Button" onclick="Button2_Click" />
<asp:TextBox ID="TextBox1"runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
二、后台代码(TestEditStyle.aspx.cs)如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Test_TestEditStyle :System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Style["background-color"] = "blue"; //背景设为蓝色
TextBox1.Style["height"] = "200px"; //高度设为200
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Style["background-color"] = "red"; //背景设为红色
TextBox1.Style["height"] = "100px";
//高度设为100
}
}
三、补充说明:
只要在标签里加上runat ="server"和ID="MyTag",就可在后台代码中直接通过设置MyTag.Style的值来控制样式。 例如:
在前端加入:
<div id="mydiv"runat="server"></div>
后台即可以直引用mydiv这个对像进行控制:
mydiv.Style["width"] = "100px";
mydiv.Style["height"] = "100px";
mydiv.Style["background-color"] ="yellow";
ASP.NET中直接用C#
动态修改CSS样式
wonsoft
(wonsoft@163.com)
使用JavaScript控制CSS样式有点麻烦,还是觉得直接使用C#操作更方便快捷,本文通过两个Button控制TextBox1的高度和背景色,以展示通过C#控制CSS样式的方法。以下是操作的实例:
一、前端代码(TestEditStyle.aspx.)如下:
<%@ Page Language="C#"AutoEventWireup="true" CodeFile="TestEditStyle.aspx.cs"Inherits="Test_TestEditStyle" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//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>
<formid="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server"Text="Button" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server"Text="Button" onclick="Button2_Click" />
<asp:TextBox ID="TextBox1"runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
二、后台代码(TestEditStyle.aspx.cs)如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Test_TestEditStyle :System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Style["background-color"] = "blue"; //背景设为蓝色
TextBox1.Style["height"] = "200px"; //高度设为200
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Style["background-color"] = "red"; //背景设为红色
TextBox1.Style["height"] = "100px";
//高度设为100
}
}
三、补充说明:
只要在标签里加上runat ="server"和ID="MyTag",就可在后台代码中直接通过设置MyTag.Style的值来控制样式。 例如:
在前端加入:
<div id="mydiv"runat="server"></div>
后台即可以直引用mydiv这个对像进行控制:
mydiv.Style["width"] = "100px";
mydiv.Style["height"] = "100px";
mydiv.Style["background-color"] ="yellow";
相关文章推荐
- ASP.NET 中直接使用C# 动态修改CSS样式
- ASP.NET中直接用C# 动态修改CSS样式 【wonsoft 】
- ASP.NET中直接用C# 动态修改CSS样式
- ASP.NET 中直接使用C# 动态修改CSS样式
- ASP.NET中直接用C# 动态修改CSS样式 【wonsoft 】
- ASP.NET中直接用C# 动态修改CSS样式
- asp.net(C#)动态修改Web.config文件
- vs2003 asp.net中直接输出wml,并利用C#做动态页面的控制,不使用微软的移动控件
- asp.net(c#)动态修改webservice的地址和端口(动态修改配置文件)
- ASP.NET给前端动态添加修改 CSS样式JS 标题 关键字
- asp.net(c#)动态修改webservice的地址和端口(动态修改配置文件)
- ASP.NET给前端动态添加修改 CSS样式JS 标题 关键字(转载)
- asp.net(C#)动态修改Web.config文件
- 在Asp.net用C#建立动态Excel(外文翻译)
- ASP.NET动态新增/减少控件的实现(C#)
- 在Asp.net用C#建立动态Excel(外文翻译)
- 动态生成随机数---ASP.net 验证码 (C#)
- ASP.NET动态生成静态页面(C#)
- [导入]在Asp.net用C#建立动态Excel
- FCKeditor 上传修改,添加对文件的类型以及大小的限制(ASP.NET C#)