引用地址: http://www.00011.com.cn/200609/200609032/32343.htm

在ASP.NET中   如:  
  <form   id="Form1"   method="post"   runat="server">  
  <tr>  
  <asp:textbox   id="txtYear"   Width="120"   Runat="server"   CssClass="TextItem"   MaxLength="4"></asp:textbox></td>  
  </tr>  
  <tr>  
  <asp:button   id="btnSearch"   Width="60"   Runat="server"   CssClass="CommonButton"   Text="検索"></asp:button>  
  </tr>  
  </from>  
  按button时,用javascript代码实现,验证textbox不为空.

问题点数:20、 回复次数:19
1楼  beiouwolf   (beiouwolf)  回复于 2006-03-17 11:30:52  得分 3

btnSearch.Attributes.Add("onCLick","if(txtYear.value==''){return   false}")
2楼  jxdyzwh   (阳光男孩)  回复于 2006-03-17 11:32:12  得分 0

同意楼上的
3楼  zmzzll   (散散心)  回复于 2006-03-17 11:37:19  得分 2

在Page_Load中写下面的代码:  
  btnSearch.Attributes.Add("onCLick","CheckNull()")  
   
  在页面(.aspx文件)中写下列代码   :  
  <SCRIPT   language="JavaScript"  
  function   DeleteAllConfirm()  
  {  
          if(txtYear.value=='')  
          {  
                  alert("年份不能为空");  
          }  
  }  
  </SCRIPT>  
   
   
  其实和1楼的方法是一样的
4楼  jerrie_1   ()  回复于 2006-03-17 11:37:31  得分 3

也可以在FORM中这样写:  
  <form   id="Form1"   method="post"   runat="server"   onsubmit="return   (document.getElementById('txtYear').value!=''">
5楼  yanlong   (西風葉落)  回复于 2006-03-17 11:41:47  得分 2

if   (document.Form1.all.textbox.value=="")  
        ...  
  else  
        ...  
  ;  
 
6楼  goodfeng110   (风云)  回复于 2006-03-17 11:50:22  得分 0

怎么好像那事件不触发一样,没用,是不是还有地方要设置??
7楼  dnboy   (computerboy)  回复于 2006-03-17 12:15:55  得分 0

同意1楼,简单
8楼  kiccleaf   (凯晰叶子(http://www.openzj.com))  回复于 2006-03-17 12:34:26  得分 0

不用JS,直接用NEt自己的控件不就行了
9楼  job_2006   (初学.net)  回复于 2006-03-17 12:40:33  得分 4

<script   language="javascript">  
  function   chkstr()  
  {  
    if(document.all.TextBox1.value=='')  
    {  
    document.all.TextBox1.focus();  
    alert('员工编号不能为空!');  
    return   false;  
    }  
    else   if(document.all.TextBox2.value=='')  
    {  
    document.all.TextBox2.focus();  
  alert('员工底薪不能为空!');  
  return   false;  
  }  
  else   if(document.all.TextBox3.value=='')  
    {  
    document.all.TextBox3.focus();  
  alert('奖励工资至少为零!');  
  return   false;  
  }  
  else   if(document.all.TextBox4.value=='')  
    {  
    document.all.TextBox4.focus();  
  alert('福利工资至少为零!');  
  return   false;  
  }  
  else   if(document.all.TextBox5.value=='')  
    {  
    document.all.TextBox5.focus();  
  alert('惩罚金至少为零!');  
  return   false;  
  }  
  else   if(document.all.TextBox6.value=='')  
    {  
    document.all.TextBox6.focus();  
  alert('住房金至少为零!');  
  return   false;  
  }  
  else   if(document.all.TextBox7.value=='')  
  {  
    document.all.TextBox7.focus();  
    alert('必须输入工资发放时间   !');  
  return   false;  
  }  
  }  
  </script>  
   
  调用:放在Page_Load中  
  Button1.Attributes.Add("OnClick",   "javascript:return   chkstr();");
10楼  goodfeng110   (风云)  回复于 2006-03-17 12:42:03  得分 0

还是不行呀,
11楼  moon_lighting   (月之影影)  回复于 2006-03-17 12:47:43  得分 1

既然你选择了.NET  
  为什么不用.NET   自己的控件RequiredFieldValidator呢
12楼  goodfeng110   (风云)  回复于 2006-03-17 13:08:38  得分 0

我怎么在页面上写javascript时,document.all   后没有txtyear这个控件名出现,这个是用<asp:textbox
13楼  goodfeng110   (风云)  回复于 2006-03-17 13:11:03  得分 0

.net里控件RequiredFieldValidator是可以验证,但有没有可以弹出像javascript里alert('');  
  这种功能,
14楼  pbwf   (书生)  回复于 2006-03-17 13:17:10  得分 0

就是自带的就可以解决.干嘛非要调出呢.如果非要.就用楼上兄弟的方法吧.用JS.
15楼  goodfeng110   (风云)  回复于 2006-03-17 13:21:53  得分 0

我要弹出窗口的这种,NET自带的可以解决吗,例如?
16楼  job_2006   (初学.net)  回复于 2006-03-17 13:30:56  得分 2

RequiredFieldValidator这种也可以实现,它有个属性叫ShowMessagebox,把它设为true  
  另外,我上面说的那种可以实现的
17楼  beiouwolf   (beiouwolf)  回复于 2006-03-17 13:34:41  得分 3

没找到?  
  你是不是有父控件的,查看源代码,看一下你的文本框ID  
   
  或者这样  
  btnSearch.Attributes.Add("onCLick","if(document.getElementById('"   &   txtYead.ClientID   &   "').value==''){return   false}")  
   
 
18楼  goodfeng110   (风云)  回复于 2006-03-17 13:49:28  得分 0

RequiredFieldValidator这种也可以实现,它有个属性叫ShowMessagebox,把它设为true  
  本身没有这属性叫ShowMessagebox  
  只有通过<asp:validationsummary   id="vsErrormessage"   Runat="server"   ShowSummary="False"   ShowMessageBox="True"></asp:validationsummary>这接收吧,但这要如果多个检查,所有信息都会出来.
19楼  Anders_lt   (突破渴望)  回复于 2006-03-17 13:52:21  得分 0

楼上的都写得很全了  
posted on 2006-12-18 15:45  ipusr  阅读(1320)  评论(0)    收藏  举报