您的位置:首页 > 编程语言 > ASP

asp.net 在后台给前台的控件设置Style属性与坐标,后台设置CSS样式,后台获取前台控件坐标

2009-03-23 12:25 776 查看
首先是后台给前台设置Style属性,设置控件坐标

前台控件:

Html代码



<asp:Label ID = "lblDSRText" Text = "当事人" runat = "server" name="label" Style="left: 300px; position: absolute;top:200px" BackColor="silver" ></asp:Label>

<asp:Label ID = "lblDSRText" Text = "当事人" runat = "server" name="label" Style="left: 300px; position: absolute;top:200px" BackColor="silver" ></asp:Label>


后台代码:

C#代码



Response.Write(lblDSRText.Style["top"] );//获得Style中top的值,获得值可以这样获得,给前台控件赋值时要用Style["Style"]

lblDSRText.Style["Style"] = "left: 500px; position: absolute;top:400px";//设置lblDSRText控件的样式

Response.Write(lblDSRText.Style["top"] );//获得Style中top的值,获得值可以这样获得,给前台控件赋值时要用Style["Style"]
lblDSRText.Style["Style"] = "left: 500px; position: absolute;top:400px";//设置lblDSRText控件的样式


解释:

lblDSRText为控件ID

Style为设置lblDSRText控件的样式

["Style"]为设置lblDSRText控件的节点

下边为转载:

教你在asp.net中动态变更CSS

在asp.net中,有的时候要动态变换CSS,比如有的时候做个性化页面,可以这样做

Html代码



<head>

  <link id="MyStyleSheet" rel="stylesheet" type="text/css" runat="server" />

</head>

<head>
  <link id="MyStyleSheet" rel="stylesheet" type="text/css" runat="server" />
</head>


  之后,在要更换CSS的页面中,使用如下代码

C#代码



Sub Page_Load(Sender As Object, E As EventArgs)

  If Not (IsPostBack)

  MyStyleSheet.Attributes.Add("href","/css/flostyle.css")

  End If

Sub Page_Load(Sender As Object, E As EventArgs)
  If Not (IsPostBack)
  MyStyleSheet.Attributes.Add("href","/css/flostyle.css")
  End If


Style与Attributes属性

作者:

1.Style属性

样式表(Cascading Style Sheets简称CSS)CSS通过其丰富的属性,将目标标签定义成各种样式,比如可以定义文字大小、标签位置等。在ASP.net中,同样可以使用样式表,不过HTML控件与web控件两者在使用方法上有所不同。下面将讨论HTML控件的样式定义。

由于HTML控件均由普通HTML标签衍生而来,所以定义HTML标签样式表的方法同样适用于HTML控件:

Html代码



<a style="color:#008000;font-size:10pt;text-decoration:none;font-style:italic"href="http://www.rencc80.com"runat="server">http://www.rencc80.com</a>

<a style="color:#008000;font-size:10pt;text-decoration:none;font-style:italic"href="http://www.rencc80.com"runat="server">http://www.rencc80.com</a>


上面为HtmlAnchor控件定义的样式表与普通<a>标签的样式定义是相同的,其显示结果也没有丝毫差别。ASP.net为每个Html 控件提供了一个Style的属性,Style属性实际上是一个样式表属性集合,通过设置Style中的属性,便能通过程序代码在程序执行过程中改变 Html控件的样式:

Html代码



<script language="c#"runat="server">

public void Page_Load(Object src,EventArgs e)

{

Anchor1.Style["color"]="#008000";

Anchor1.Style["font-size"]="10pt";

Anchor1.Style["text-decoration"]="none";

Anchor1.Style["font-style"]="italic";

}

</script>

<html><head><title></title></head>

<body id="Body1"runat="server">

<a id="Anchor1"href="http://www.rencc80.com"runat="server">http://www.rencc80.com</a>

</body>

</html>

<script language="c#"runat="server">
public void Page_Load(Object src,EventArgs e)
{
Anchor1.Style["color"]="#008000";
Anchor1.Style["font-size"]="10pt";
Anchor1.Style["text-decoration"]="none";
Anchor1.Style["font-style"]="italic";
}
</script>
<html><head><title></title></head>
<body id="Body1"runat="server">
<a id="Anchor1"href="http://www.rencc80.com"runat="server">http://www.rencc80.com</a>
</body>
</html>


它所显示的结果与直接定义Style是一样的。

2.Attributes属性

Attributes属性实质上是一个Server控件(包括Html控件、Web控件、用户控件)的属性集合。它的设置方式为:

C#代码



Control.Attributes["attribute"]=Value;

Control.Attributes["attribute"]=Value;


控件的属性值与属性值可以通过Attributes任意指定,ASP.net程序会将其按原样发送到浏览器解释。

注意以下几点:

因可以任意指定属性,故对于控件来说,有些指定的属性是不合法的,那么这种属性就是无效的。如:假设当前操作的控件为HtmlImage,名为 image1,假设通过Attribute给其指定一个Text属性,属性值为“你好”。因为HtmlImage控件将会被转化为<img> 标签,而指定的Text属性将按原样发送,所以就会出现<img Text="你好"...>这种代码,显然,<img>标签根本没有Text属性,所以这个属性将会被浏览器忽略,不予理睬。

指定属性必须为Server控件对应的HTML标签所支持的属性,否则浏览器也会将不能达到。例如:HtmlImage控件有Title属性,它的作用是当图象没有正确加载时,显示在图象位置的文字,可以通过HtmlImage.Title=“描述文字”设置。通常所犯的错误就是在Attributes设置时,直接使用Title,如:

C#代码



HtmlImage.Attributes["Title"]="描述文字";//(这是错误的)

HtmlImage.Attributes["Title"]="描述文字";//(这是错误的)


按照原样发送的规则,那么发送至浏览器的代码就会是<img title="描述文字"...>,而<img>标签中没有title属性,所以这个属性也是不合法的。正确的设置方法是:

C#代码



HtmlImage.Attributes["alt"]="描述文字";//(正确)

HtmlImage.Attributes["alt"]="描述文字";//(正确)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐