asp.net控件开发基础(6)
2006-09-18 16:57
351 查看
上一篇讲了关于属性方面的一些东西 ,这次讲的是关于样式.
位于WebControls命名空间的style类为顶级样式类.大部分标准控件都拥有其样式属性.
1.下面为设置样式方法
(1)你可以直接设置控件样式
Button1.BackColor = System.Drawing.Color.Red;
(2)通过获取web控件的样式集合来设置
Button1.ControlStyle.BackColor = System.Drawing.Color.Red;
(3)通过设置样式类,利用WebControl类的ApplyStyle方法来复制非空样式,并改写现有样式
myStyle.BackColor = System.Drawing.Color.Red;
Button1.ApplyStyle(myStyle);
(4)一直定义样式表属性,不使用控件属性,与定义HTML样式相同.
style="background-color: red"
下面引出话题,为什么要使用样式?大家知道定义样式可以使用统一风格,定义好的样式,可以重复使用.再回来看上面设置样式方法.
2.了解WebControl.BackColor和Style.BackColor
(1)和(2)是差不多的.但(3)则不同,(3)的定义方法有通用性,你可以定义一种样式,然后利用控件的ApplyStyle方法来引用样式.给样式编程提供了方面
WebControl类定义了通用的样式.(1)和(2)使用的样式属性为
WebControl.BackColor
(3)则不同,使用的为
Style.BackColor
3.自定义样式属性
刚开始就讲了style类为通用的顶级样式类,但需求是会发生变化的. 好了,下面真正开始编码了.
下面以改写label控件为例子
(1)改写样式属性,让其默认背景为红色,相信大家一定看的懂
示例一
namespace CustomComponents
namespace CustomComponents
protected override Style CreateControlStyle()
protected override Style CreateControlStyle()
namespace CustomComponents
protected void Page_Load(object sender, EventArgs e)
public override Color BackColor
public virtual string BackImageUrl
<custom:CreditCardForm6 BackColor="Black" ForeColor="White" runat="server" ID="example"
Font-Bold="true" Font-Italic="true" GridLines="None" CellSpacing="5"
BackImageUrl="images4.bmp" Font-Size="Larger"
BorderColor="Yellow" BorderWidth="20px" BorderStyle="Ridge" HorizontalAlign="NotSet" EnableViewState="False" />
效果如下
public class LabelStyle :Style
public class ImageLabel4 : Label
//判断视图状态是否为空
internal bool IsSet(string key)
public override bool IsEmpty
LabelStyle a = new LabelStyle();
a.ImageUrl = "images4.bmp";
a.BackColor = System.Drawing.Color.Red;
ImageLabel4_1.ApplyStyle(a);
Style类有三个操作样式的方法,复制,合并和清除样式.WebControl类也一样.重写一下,我们的目的就达到了.看下面代码
示例8
protected override void FillStyleAttributes(CssStyleCollection attributes, IUrlResolutionService urlResolver)
base.FillStyleAttributes(attributes, urlResolver);
attributes.Add(HtmlTextWriterStyle.BackgroundImage, ImageUrl);
}
关于对样式的理解基本的东西就先写这么多吧.大家看完了再回去看看,注意步骤就可以了.重在理解.
好久没写了发现自己写的又挺长的,写的好累呀...希望对大家有帮助吧.
示例代码下载
参考文章:
<<asp.net服务器控件开发技术与示例>>
还有国外的两本书,一本是微软的,一本是appess的.
还有刚wrox新出的一本.名字就不多说了,想学的人,肯定找的到.
还有两篇文章
http://dev.yesky.com/msdn/335/2507335.shtml
http://dev.yesky.com/msdn/284/2505784.shtml
位于WebControls命名空间的style类为顶级样式类.大部分标准控件都拥有其样式属性.
1.下面为设置样式方法
(1)你可以直接设置控件样式
Button1.BackColor = System.Drawing.Color.Red;
(2)通过获取web控件的样式集合来设置
Button1.ControlStyle.BackColor = System.Drawing.Color.Red;
(3)通过设置样式类,利用WebControl类的ApplyStyle方法来复制非空样式,并改写现有样式
myStyle.BackColor = System.Drawing.Color.Red;
Button1.ApplyStyle(myStyle);
(4)一直定义样式表属性,不使用控件属性,与定义HTML样式相同.
style="background-color: red"
下面引出话题,为什么要使用样式?大家知道定义样式可以使用统一风格,定义好的样式,可以重复使用.再回来看上面设置样式方法.
2.了解WebControl.BackColor和Style.BackColor
(1)和(2)是差不多的.但(3)则不同,(3)的定义方法有通用性,你可以定义一种样式,然后利用控件的ApplyStyle方法来引用样式.给样式编程提供了方面
WebControl类定义了通用的样式.(1)和(2)使用的样式属性为
WebControl.BackColor
(3)则不同,使用的为
Style.BackColor
3.自定义样式属性
刚开始就讲了style类为通用的顶级样式类,但需求是会发生变化的. 好了,下面真正开始编码了.
下面以改写label控件为例子
(1)改写样式属性,让其默认背景为红色,相信大家一定看的懂
示例一
namespace CustomComponents
namespace CustomComponents
protected override Style CreateControlStyle()
protected override Style CreateControlStyle()
namespace CustomComponents
protected void Page_Load(object sender, EventArgs e)
public override Color BackColor
public virtual string BackImageUrl
<custom:CreditCardForm6 BackColor="Black" ForeColor="White" runat="server" ID="example"
Font-Bold="true" Font-Italic="true" GridLines="None" CellSpacing="5"
BackImageUrl="images4.bmp" Font-Size="Larger"
BorderColor="Yellow" BorderWidth="20px" BorderStyle="Ridge" HorizontalAlign="NotSet" EnableViewState="False" />
效果如下
public class LabelStyle :Style
public class ImageLabel4 : Label
//判断视图状态是否为空
internal bool IsSet(string key)
public override bool IsEmpty
LabelStyle a = new LabelStyle();
a.ImageUrl = "images4.bmp";
a.BackColor = System.Drawing.Color.Red;
ImageLabel4_1.ApplyStyle(a);
Style类有三个操作样式的方法,复制,合并和清除样式.WebControl类也一样.重写一下,我们的目的就达到了.看下面代码
示例8
protected override void FillStyleAttributes(CssStyleCollection attributes, IUrlResolutionService urlResolver)
base.FillStyleAttributes(attributes, urlResolver);
attributes.Add(HtmlTextWriterStyle.BackgroundImage, ImageUrl);
}
关于对样式的理解基本的东西就先写这么多吧.大家看完了再回去看看,注意步骤就可以了.重在理解.
好久没写了发现自己写的又挺长的,写的好累呀...希望对大家有帮助吧.
示例代码下载
参考文章:
<<asp.net服务器控件开发技术与示例>>
还有国外的两本书,一本是微软的,一本是appess的.
还有刚wrox新出的一本.名字就不多说了,想学的人,肯定找的到.
还有两篇文章
http://dev.yesky.com/msdn/335/2507335.shtml
http://dev.yesky.com/msdn/284/2505784.shtml
相关文章推荐
- asp.net控件开发基础(1)
- asp.net控件开发基础(7)
- asp.net控件开发基础
- 【转】Asp.net控件开发学习笔记整理篇 - 控件开发基础
- asp.net控件开发基础(1)
- asp.net控件开发基础(4)
- asp.net控件开发基础(6)
- asp.net控件开发基础(18) --------让DadaSource接受过多的数据源
- asp.net控件开发基础系列
- asp.net控件开发基础(2)
- asp.net控件开发基础(转)
- asp.net控件开发基础(1) -- Render、输出html-asp.net关注
- asp.net控件开发基础(1)
- asp.net控件开发基础(11)
- asp.net控件开发基础(10)
- asp.net控件开发基础(2)
- asp.net控件开发基础(4)
- asp.net控件开发基础(7)
- asp.net控件开发基础十
- asp.net控件开发基础系列