AttributeCollection类与Attributes.Add方法的使用[转载]

控件属性集合: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属性中的方法,遍历控件的属性并打印。

[csharp]  view plain  copy
  1. <html xmlns="http://www.w3.org/1999/xhtml" >  
  2. <head runat="server">  
  3.     <title>无标题页</title>  
  4. </head>  
  5. <body>  
  6.     <form id="form1" runat="server">  
  7.     <div>  
  8.          <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>  
  9.         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>  
  10.     </form>  
  11. </body>  
  12. </html>  

 

[csharp]  view plain  copy
  1. protected void Button1_Click(object sender, EventArgs e)  
  2.  {  
  3.      AttributeCollection myac = TextBox1.Attributes; //创建当前状态集合  
  4.      Response.Write(myac.Count);//显示当前集合中的项数  
  5.      myac.Add("Name""cgj");//添加属性到集合中  
  6.      myac.Add("OnClick""javascript:alert('Hello');");  
  7.      IEnumerator myenum = myac.Keys.GetEnumerator();  
  8.      while (myenum.MoveNext())//遍历属性  
  9.      {  
  10.          Response.Write(myenum.Current.ToString());  
  11.      }  
  12.      myac.Remove("name");//移除集合中的属性键  
  13.  }  


 

理解了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;
       }
    }

//明白了吗。。这是一个用来判断两次密码输入是否一致的函数


原文地址:http://blog.csdn.net/byondocean/article/details/5799863

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值