遍历ASP.NET页面上的TextBox控件,并为其添加鼠标事件

    前台代码:(主要是TextBox控件和CSS)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <style>
      input
    {
     border:1px solid #E0E0E0;
     margin:1px;
    }
    .NormalTxt
    {
     border:1px solid #E0E0E0;
    }
    .SpecialTxt
    {
     border:1px solid #FF8000;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></div>
    </form>
</body>
</html>

后台代码:

 protected void Page_Load(object sender, EventArgs e)
    {
        int nPageControls = Controls.Count;
        for (int i = 0; i < nPageControls; i++)   //父控件层
        {
            foreach (Control ctl in Page.Controls[i].Controls)    //子控件层
            {
                if (ctl is TextBox)
                {
                    TextBox tb = (TextBox)this.FindControl(ctl.ID);
                    tb.Attributes.Add("onfocus", "this.className='SpecialTxt'");   //为控件添加JavaScript事件处理
                    tb.Attributes.Add("onblur", "this.className='NormalTxt'");
                }
            }
        }
    }

posted @ 2009-07-16 08:57  锐意  阅读(909)  评论(0编辑  收藏  举报