如何使页面中所有的textbox失去焦点

类别:Asp 点击:0 评论:0 推荐:

要是页面中有几个textbox还好说,直接设置他的readonly属性即可.但是要是很多呢?那就采用把所有的textbox包含到一个里面.代码如下:

<script runat="server" language="C#">
       void Page_Load(object Sender, EventArgs E)
       {      
                string  a="3";
               foreach (object item in myform.Controls)//定义个类item,包含myform里的所有控件
               {
                       if (item is TextBox)
                       {
                               TextBox tbx = (TextBox)item;//类的引用
                               tbx.Text = "overred";
                               Response.Write(tbx);
                               if(a.ToString()=="3")
                               {tbx.Enabled=false; }
                                else
                               {tbx.Enabled=true; }
                       }
               }
       }
</script>

<html>
<body>
       <form id="myform" runat="server">
               请输入您的姓名:
               <asp:textbox id="username" runat="server" />
               <p>请输入您的年龄:
               <asp:textbox id="Age" runat="server" />
               <asp:button id="enter" text="确定" runat="server" />
               <p>
               <asp:label id="Message"  runat=server/>
       </form>
</body>
</html>

输出如下:

System.Web.UI.WebControls.TextBoxSystem.Web.UI.WebControls.TextBox  请输入您的姓名:

请输入您的年龄:

本文地址:http://com.8s8s.com/it/it7216.htm