AttributeCollection类与Attributes.Add方法的使用
来源:互联网 发布:北京立方米网络 编辑:程序博客网 时间:2024/05/16 12:12
控件属性集合: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属性中的方法,遍历控件的属性并打印。
<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"> <title>无标题页</title></head><body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div> </form></body></html>
protected void Button1_Click(object sender, EventArgs e) { AttributeCollection myac = TextBox1.Attributes; //创建当前状态集合 Response.Write(myac.Count);//显示当前集合中的项数 myac.Add("Name", "cgj");//添加属性到集合中 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("onblue", "window.Label1.style.backgroundColor='#000000';");
this.TextBox1.Attributes.Add("onblur","this.style.display='none'");
javascript事件:
onClick 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击
onDblClick 鼠标双击事件
onMouseDown 鼠标上的按钮被按下了
onMouseUp 鼠标按下后,松开时激发的事件
onMouseOver 当鼠标移动到某对象范围的上方时触发的事件
onMouseMove 鼠标移动时触发的事件
onMouseOut 当鼠标离开某对象范围时触发的事件
onKeyPress 当键盘上的某个键被按下并且释放时触发的事件.[注意:页面内必须有被聚焦的对象]
onKeyDown 当键盘上某个按键被按下时触发的事件[注意:页面内必须有被聚焦的对象]
onKeyUp 当键盘上某个按键被按放开时触发的事件[注意:页面内必须有被聚焦的对象]
this.txtSugStartDate.Attributes.Add("onclick", "return showCalendar('" + this.txtSugStartDate.ClientID + "','y/mm/dd') ");
this.txtSugStartDate.Attributes.Add("readonly", "true");
this.txtSugEndDate.Attributes.Add("onclick", "return showCalendar('" + this.txtSugEndDate.ClientID + "','y/mm/dd')");
this.txtSugEndDate.Attributes.Add("readonly", "true");
this.txtProcessStartDate.Attributes["onclick"] = "return showCalendar('" + this.txtProcessStartDate.ClientID + "','y/mm/dd')";
this.txtProcessStartDate.Attributes["readonly"] = "true";
this.txtProcessEndDate.Attributes["onclick"] = "return showCalendar('" + this.txtProcessEndDate.ClientID + "','y/mm/dd')";
this.txtProcessEndDate.Attributes["readonly"] = "true";
使用举例:
//首先要在PageLoad()事件中注册属性
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Button1.Attributes.Add("onclick", "return checkSame()");//为Button1添加onclick()事件 ,Button为服务器控件
}//注意:checkSame()这是一个写在aspx面页的js函数,必须有返回值,为:true 或 false
}
//接着写Button1的onclick事件,如果刚才的checkSame()返回为true则招行下面的事件,否则不执行
protected void Button1_Click(object sender, ImageClickEventArgs e)
{
SqlParameter[] Params = new SqlParameter[2];
Params[0] = dbs.MakeInParams("@uid", SqlDbType.VarChar, 10, Session["Uid"].ToString());
Params[1] = dbs.MakeOutParms("@Upwd", SqlDbType.VarChar, 10);
if (dbs.ExecuteNonQuery(CommandType.StoredProcedure, "selectPwd", Params) > 0)
{
string userPwd = Params[1].Value.ToString();
if (userPwd != this.old_pwd.Text)
{
Response.Write("<script>alert('原始密码错误!')</script>");
}
else
{
}
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('操作失败!')</script>");
}
}
//呵呵。。再写一个js试例吧
function checkSame()
{
var Obj1=document.getElementById ("new_pwd").value;
var Obj2=document.getElementById ("re_new_pwd").value;
if(Obj1!=Obj2)
{
alert("两次密码输入不一致!");
document.getElementById("new_pwd").focus();
return false;
}
else
{
return true;
}
}
//明白了吗。。这是一个用来判断两次密码输入是否一致的函数
- AttributeCollection类与Attributes.Add方法的使用
- AttributeCollection类与Attributes.Add方法的使用[转载]
- Button1.Attributes.Add()方法小结
- Button1.Attributes.Add()方法小结
- Button1.Attributes.Add()方法小结
- Attributes.Add用途与用法
- Attributes.Add用途与用法
- Attributes.add
- Attributes.Add
- Attributes.Add()
- 使用Attributes.Add(event,fun)方法为服务器控件动态添加属性【鸡蛋】
- 关于TextBox1.Attributes.Add的问题
- Attributes的使用
- ArrayList的add()方法使用
- ASP.NET 验证控件与Attributes.Add 脚本并存
- Calendar类add()与roll()方法的区别
- sqlalchemy使用session的add方法
- Attributes.Add() Attributes.Remove() Style.Add() Style.Remove()
- 【荐】小型办公团队一体化打印解决方案
- eclipse/svn 入门
- C#处理图片的范例
- MSSQL Server 2000
- HashTable和HashMap的区别
- AttributeCollection类与Attributes.Add方法的使用
- SipDroid开源软件的研究
- PI system
- EOF学习
- 如何让Google Chrome支持网银等IE控件——IE Tab插件
- sprintf使用详解(转)
- i am a tiny bird-7
- 进度条
- C语言文件传输客户端