动态更改css元素值 [英] change css element value dynamically

查看:158
本文介绍了动态更改css元素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,
我正在创建一个网站,我想通过c#或vb动态更改css元素的值.我该如何更改?
前任.我想通过c#或vb更改标题背景色.

Hello friends,
I am creating one website for which i want to change css element value dynamically through c# or vb. how do i change it?
ex. i want to change header back-color through c# or vb.

推荐答案

有很多方法可以使用来自C#.Net代码的CSS动态布局

在下面的链接中,我展示了一种非常简单的使用方法

http://hemantrautela.blogspot.in/2012/09/manage-css-using-cnet-aspnet-code-at.html [
There are many way to use CSS dynamiclay from C#.Net Code

at below link I show a very simple way to use it

http://hemantrautela.blogspot.in/2012/09/manage-css-using-cnet-aspnet-code-at.html[^]


如果您想在运行时通过服务器端代码更改CSS元素,需要通过添加"runat = server"将该控件转换为服务器控件,或者从后面的代码中注入JavaScript来更改样式.

选项#1:您将标头定义为服务器控件,并为标头提供ID
您可以直接访问控件的CssClass属性并将其设置为所需的

选项#2:您将JavaScript从后面的代码插入页面中
您可以使用:
MSDN:RegisterClientScriptBlock方法 [ MSDN:RegisterStartupScript方法 [
If you want to change the CSS element at runtime through server side code then you need to convert that control into server control by adding ''runat=server'' OR inject JavaScript from code behind to change the style.

Option #1: You define the header as server control and provide ID to the header
You can directly, access the CssClass property of the control and set to desired one

Option #2: You inject the JavaScript into page from code behind
You can do it using:
MSDN: RegisterClientScriptBlock Method[^] OR
MSDN: RegisterStartupScript Method[^]

In the JS method, find the header element and access it''s style-backcolor attribute to modify it.

Try!


我们有几种方法,

最简单的方法
There are several ways we do,

Simplest way
textBox1.Attributes["class"] = "class name";
//textBox1 is a Text Box Server Control and "class name" is your custom class



另一种方法是使用Web Extension,



Another way is to use Web Extension,

static class WebControlsExtensions
 {
     public static void AddCssClass (this WebControl control, string cssClass)
     {
         control.CssClass += " " + cssClass;
     }
     public static void RemoveCssClass (this WebControl control, string cssClass)
     {
         control.CssClass = control.CssClass.replace(" " + cssClass, "");
     }
 }


ctl.AddCssClass("ReadOnly");
ctl.RemoveCssClass("ReadOnly");

希望这会有所帮助.
欢呼


ctl.AddCssClass("ReadOnly");
ctl.RemoveCssClass("ReadOnly");

Hope this helps.
cheers


这篇关于动态更改css元素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆