控件属性集合:AttributeCollection类
AttitudeCollection隶属于namespace System.Web.UI命名空间。AttributeCollection类属于集合类,具有其他集合所共有的一些属性。AttributeCollection类用来封装服务器控件的所有属性,可实现对服务器属性集合的添加和删除。控件的属性包括颜色、样式、名称、事件等,这些属性都可以通过AttributeCollection类访问到。
AttributeCollectin类的主要目的是使开发人员可以通过编程的方式访问服务器的所有属性,并实现对这些属性的编辑。
语法定义:
public sealed class AttributeCollection
AttitudeCollection类的构造函数:
public AttributeCollection(StateBag bag)
参数“bag”封装着控件的所有属性键和值
AttributeCollection类的使用方法如下:
AttributeCollection myac = TextBox1.Attributes;
TextBox1之所有拥有Attibutes属性,是因为TextBox1继承于命名空间 System.Web.UI.WebControls的WebControl类,而Attributes是WebControl类的一个属性:
public System.Web.UI.AttributeCollection Attributes { get; }
属性详解:
Count:属性集合中的属性数量
CssStyle:服务器控件的样式
Item:获取控件指定的属性
Keys:获取控件属性的键集合
典型应用:动态添加属性并遍历属性集
AttributeCollection类主要的功能是提供对控件属性的操作。本例演示如何在运行时动态添加属性,同时通过Keys属性中的方法,遍历控件的属性并打印。
前台代码:<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
后台代码:
using System;
using System.Collections;
using System.Web.UI;
namespace WebApplication1
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void Button1_Click(object sender, EventArgs e)
{
AttributeCollection myac = TextBox1.Attributes; //创建当前状态集合
Response.Write(myac.Count);//显示当前集合中的项数
myac.Add("onblur", "window.TextBox1.style.backgroundColor='#000000';");//onblur:失去焦点事件
myac.Add("OnClick", "javascript:alert('Hello');");
IEnumerator myenum = myac.Keys.GetEnumerator();
while (myenum.MoveNext())//遍历属性
{
Response.Write(myenum.Current.ToString());
}
myac.Remove("Name");//移除集合中的属性键
}
}
}
理解了AttitudeCollection集合类,我们对Attributes.Add方法的使用就会有更加深刻的理解,下面我们演示Attributes.Add用途与用法。
Attributes.Add("javascript事件","javascript语句");
如:
this.TextBox1.Attributes.Add("onblur", "window.TextBox1.style.backgroundColor='#000000';");
this.TextBox1.Attributes.Add("onblur","this.style.display='none'");
javascript事件:
onClick 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击
onDblClick 鼠标双击事件
onMouseDown 鼠标上的按钮被按下了
onMouseUp 鼠标按下后,松开时激发的事件
onMouseOver 当鼠标移动到某对象范围的上方时触发的事件
onMouseMove 鼠标移动时触发的事件
onMouseOut 当鼠标离开某对象范围时触发的事件
onKeyPress 当键盘上的某个键被按下并且释放时触发的事件.[注意:页面内必须有被聚焦的对象]
onKeyDown 当键盘上某个按键被按下时触发的事件[注意:页面内必须有被聚焦的对象]
onKeyUp 当键盘上某个按键被按放开时触发的事件[注意:页面内必须有被聚焦的对象]
使用举例:
后台代码:using System;
namespace WebApplication1
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Button1.Attributes.Add("onclick", "return check()");//为Button1添加onclick()事件 ,Button为服务器控件
}//注意:check()这是一个写在aspx面页的js函数,必须有返回值,为:true 或 false
//如果刚才的check()返回为true则招行下面的Button1_Click事件,否则不执行
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("Button1_Click事件执行了");
}
}
}
前台js:
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script>
function check() {
return true;
}
</script>
</head>