当前位置:编程学习 > asp >>

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

要是页面中有几个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

 请输入您的姓名:

请输入您的年龄:


补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,