在html页面中我有几十个textBox控件,有下面一段代码:
 <script   language="javascript">   
        function   regInput(obj,   reg,   inputStr)   
        {  
           
            var   docSel =   document.selection.createRange()   
            if   (docSel.parentElement().tagName   !=   "INPUT") 
                return   false   
            oSel   =   docSel.duplicate()   
            oSel.text   =   ""   
            var   srcRange =   obj.createTextRange()   
            oSel.setEndPoint("StartToStart",   srcRange)   
            var   str   =   oSel.text   +   inputStr   +   srcRange.text.substr(oSel.text.length)   
            return   reg.test(str)   
         }   
    </script>   
如果我只有一个TextBox的话我可以这样调用:如下
asp:TextBox ID="txtP95" runat="server" Width="25px"
                onKeyPress =   "return   regInput(this, /^\d*\.?\d{0,2}$/, String.fromCharCode(event.keyCode))" 
                onpaste =   "return   regInput(this, /^\d*\.?\d{0,2}$/, window.clipboardData.getData('Text'))"   
                ondrop =   "return   regInput(this, /^\d*\.?\d{0,2}$/, event.dataTransfer.getData('Text'))"></asp:TextBox>
但是现在一个页面中有几十个TextBox,如果象第一个TextBox那样调用的话,那么每个TextBox后面都有这么一大串代码。也就是说我想页面一加载,每个TextBox都能调用上面的TextBox
请大家帮个忙

解决方案 »

  1.   

    上面的最后一句错了,应该是页面一加载。每个TextBox都能调用javascript
      

  2.   

    前台用JS动态给所有的TextBox注册事件
    或在后台给所有的TextBox的Attributes添加JS事件
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    foreach (Control c in this.Page.Controls)
                    {
                        if (c is TextBox)
                        {
                            TextBox tb = c as TextBox;
                            tb.Attributes["onKeyPress"] = @"return  regInput(this, /^\d*\.?\d{0,2}$/, String.fromCharCode(event.keyCode))";
                            tb.Attributes["onpaste"] = @"return  regInput(this, /^\d*\.?\d{0,2}$/, window.clipboardData.getData('Text'))";
                            tb.Attributes["ondrop"] = @"return  regInput(this, /^\d*\.?\d{0,2}$/, event.dataTransfer.getData('Text'))";
                        }
                    }
                }
            }
      

  3.   

    可是我用foreach (Control c in this.Page.Controls)取不到TextBox,这是为什么呢
      

  4.   

    //你的TextBox是放在别的容器里?如放在Pane里,就在panel里找
    用foreach (Control c in this.Panel1.Controls)
      

  5.   

    没放在Panel里,就放在table里啊,但是调不到
      

  6.   

    呵呵,我找到答案了,应该是这样写的foreach (Control c in this.Page.Form.Controls)
      

  7.   

    用foreach (Control c in this.form1.Controls)
    form1是页面的表单ID