今天在做项目时遇到对于文本框验证的问题,需要在前台用js验证,问题是要验证的控件是在datalist控件中的textbox控件。
开始觉得没办法下手去做。经过头的指点,感觉有点头绪。
<script type="text/javascript">
  function CheckValue()
  {
   var attributeName = document.getElementById("<%=this.attrATTRIBUTE_NAME.ClientID %>");
   if (attributeName.value.trim() == "")
   {
    alert("Attribute name is not null");
    attributeName.focus();
    return false;
   }
   var dlAttributes = document.getElementById("<%=this.dlAttributes.ClientID %>");
   if (dlAttributes != null)
   {
    var tbAttributes = dlAttributes.getElementsByTagName("input");
    for (var i = 0; i < tbAttributes.length; i++)
    {
     if (tbAttributes[i].id.indexOf("txtAttributeValue") > 0)
     {
      if (tbAttributes[i].value.trim().length == 0)
      {
       alert("Attribute value is not null");
       tbAttributes[i].focus();
       return false;
      }
     }
    }
   }
   return true;
  } 
 </script>
js代码如上:原来以为只有intput控件才可以使用getElementsByTagName方法。现在知道服务器端控件在解析时也解析成个intput控件,它当然也是可以使用的。
刚开始没有加if (dlAttributes != null)这个条件,结果在运行时报错“ error: object is not null”.费了好大力气才知道是这个地方出错了。
自己得从头开始学起啊,会的东西太少了。