当前位置:编程学习 > C#/ASP.NET >>

ASP.NET中对表单输入行有选择验证(2)

答案:     好,下面我们来测试一下我们的代码,先在region name中输入一个值,如china,并提交,输出如下:
  
  
  
  可以看到,这个时候,我们没有输入开始日期和结束日期,但系统没有提示出错信息,同样道理,只输入开始日期和结束日期,而不输入region name,也不会有提示出错信息。
  
    程序的主要代码如下:
  
    DisableClientValidation.ASPx:
  
  <form id="Form1" method="post" runat="server">
  <TABLE id="Table1" cellSpacing="0" cellPadding="0" width="504"
  align="center" borderColorLight="#996633" border="1">
  <TR>
  <TD style="FONT-WEIGHT: bold" align="center" colSpan="3">Region Wise Reports</TD>
  </TR>
  <TR>
  <TD>Region Name</TD>
  <TD>
  <asp:TextBox id="txtRegionName" runat="server"></asp:TextBox></TD>
  <TD>
  <asp:RequiredFieldValidator id="rvRegion" runat="server" ControlToValidate="txtRegionName" ErrorMessage="Region is required"></asp:RequiredFieldValidator></TD>
  </TR>
  <TR>
  <TD></TD>
  <TD></TD>
  <TD>
  <asp:Button id="cmdRegion" runat="server" Text="Get Report"></asp:Button></TD>
  </TR>
  </TABLE>
  <br>
  <br>
  <TABLE id="Table2" cellSpacing="0" cellPadding="0" width="504" border="1"
  align="center" borderColorLight="#996633">
  <TR>
  <TD style="FONT-WEIGHT: bold" align="center" colSpan="3">Date Wise Reports</TD>
  </TR>
  <TR>
  <TD>Start Date</TD>
  <TD>
  <asp:TextBox id="txtStartDate" runat="server"></asp:TextBox></TD>
  <TD>
  <asp:RequiredFieldValidator id="rvStartDate" runat="server" ControlToValidate="txtStartDate" ErrorMessage="Start Date is required"></asp:RequiredFieldValidator></TD>
  </TR>
  <TR>
  <TD>End Date</TD>
  <TD>
  <asp:TextBox id="txtEndDate" runat="server"></asp:TextBox></TD>
  <TD>
  <asp:RequiredFieldValidator id="rvEndDate" runat="server" ControlToValidate="txtEndDate" ErrorMessage="End Date is required"></asp:RequiredFieldValidator></TD>
  </TR>
  <TR>
  <TD style="WIDTH: 139px"></TD>
  <TD></TD>
  <TD>
  <asp:Button id="CmdDate" runat="server" Text="Get Report"></asp:Button></TD>
  </TR>
  </TABLE>
  </form>
  <script language="javascript">
  function disableScript()
  {
   for(i=0;i< Page_Validators.length;i++)
   {
    //Page_Validators[i].isvalid=true;
    ValidatorEnable(Page_Validators[i], false)
   }
  }
  
  function enableRegionValidators()
  {
   for(i=0;i< Page_Validators.length;i++)
   {
    ValidatorEnable(Page_Validators[i], false)
   }
   ValidatorEnable(rvRegion, true)
  }
  
  function enableDateValidators()
  {
   for(i=0;i< Page_Validators.length;i++)
   {
    ValidatorEnable(Page_Validators[i], false)
   }
   ValidatorEnable(rvStartDate, true)
   ValidatorEnable(rvEndDate, true)
  }
  </script>
  <asp:Label id="lblReport" style="Z-INDEX: 101; LEFT: 144px; POSITION: absolute; TOP: 344px"
  runat="server" Width="344px" Height="24px"></asp:Label>
  DisableClientValidation.aspx:.cs主要代码:
  private void Page_Load(object sender, System.EventArgs e)
  {
   cmdRegion.Attributes.Add("onclick","enableRegionValidators();");
   CmdDate.Attributes.Add("onclick","enableDateValidators();");
  }
  private void cmdRegion_Click(object sender, System.EventArgs e)
  {
   rvStartDate.IsValid=true;
   rvEndDate.IsValid=true;
   lblReport.Text = "Region Wise Report For " + txtRegionName.Text.ToUpper();
  }
  
  private void CmdDate_Click(object sender, System.EventArgs e)
  {
   rvRegion.IsValid=true;
   lblReport.Text = "Date Wise Report Betwee " + txtStartDate.Text.ToUpper() + " And " + txtEndDate.Text.ToUpper();
  }
  }
  

上一个:在VS.NET 2005中体验clickonce技术(2)
下一个:利用ASP.NET 2.0创建自定义Web控件(5)

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,