背景:
阅读新闻

验证控件笔记

  作者: 今日评论: [字体: ]
1)from:http://dev.csdn.net/article/56/56880.shtm 

在asp.net 1.1中,对于验证类控件,在使用时,遇到有的不需要验证的控件时,是十分麻烦的,就是说不可能有选择验证某些控件,而在asp.net 2.0中,新增加了validationgroup属性,可以指定验证某些控件,例子如下:

<html>

<body>

     <form runat=“server”>

          <asp:textbox id=“TextBox1” runat=“server”/>

          <asp:requiredfieldvalidator ValidationGroup=“Group1”

                                                       ErrorText=“Need to Fill in Value!”

                                                       ControlToValidate=“TextBox1”

                                                       runat=“server”/>

 

            <asp:textbox id=“TextBox2” runat=“server”/>

            <asp:requiredfieldvalidator ValidationGroup=“Group2”

                                                         ErrorText=“Need to Fill in Value!”

                                                         ControlToValidate=“TextBox2”

                                                         runat=“server”/>

 

            <asp:button text=“Group1” ValidationGroup=“Group1” runat=“server”/>

            <asp:button text=“Group2” ValidationGroup=“Group2” runat=“server”/>

     </form>

</body>

</html>

 

2)from:http://www.dotblogs.com.tw/pbnttttt/archive/2009/09/09/10511.aspx

 

驗証控制項可以針對要驗証的控制項做群組的驗証設定,針對多個群組如何一起做驗証呢?

01 valid a group<br />    
02 <asp:TextBox ID="TextBox1" runat="server" ValidationGroup="a"></asp:TextBox>
03 <asp:RangeValidator ID="RangeValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="10~100" MaximumValue="100" 
04                     MinimumValue="10" Type="Integer" ValidationGroup="a" />
05 <asp:Button ID="Button1" runat="server" Text="a group 送出" ValidationGroup="a" />
06 <br />
07 valid b group<br />
08 <asp:TextBox ID="TextBox2" runat="server" ValidationGroup="b"></asp:TextBox>
09 <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" ErrorMessage="*" 
10                             ValidationGroup="b" />
11 <asp:Button ID="Button2" runat="server" Text="b group 送出" ValidationGroup="b" />
12 <br />
13 valid c group<br />
14 <asp:TextBox ID="TextBox3" runat="server" ValidationGroup="c"></asp:TextBox>
15 <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBox3" ErrorMessage="*" 
16                             ValidationGroup="c" />
17 <asp:Button ID="Button3" runat="server" Text="c group 送出" ValidationGroup="c" />
18 <br />
19 <br />
20 <asp:Button ID="Button4" runat="server" Text="全部送出" onclick="Button4_Click" onclientclick="return myValid();" />

 

Client端:
在全部送出的button設定onclientclick的js函式

 
1 <script type="text/javascript">
2         function myValid() {
3             for (i = 0; i < Page_Validators.length; i++) {
4                 //Page_ClientValidate位於WebResource.axd,用來驗証群組的函式
5                 if (!Page_ClientValidate(Page_Validators[i].validationGroup))
6                     return false;
7             }
8         }
9 </script>

 

Server 端

 
01 protected void Button4_Click(object sender, EventArgs e)
02 {
03     Validate();
04     if (IsValid)
05     {
06         Response.Write("OK");
07     }
08     else
09     {
10         Response.Write("no");
11     }
12 }


都要一起設定唷~雙管齊下才安全!!

 

 

from:http://hi.baidu.com/zengxiangzheng/blog/item/eb9101f2c560395a342acce1.html

asp.net中使用Page.Validate()和Page.IsValid进行服务器端验证

     通常我们在ASPX上面设置各种验证控件就可以对表单输入项进行检查,但这个功能在客户端关掉JavaScript之后,就over了。

        为了防止某些坏人搞破坏,ASP.NET在Page_Load事件之后,会执行Page.Validate()去启动验证控件的服务器端验证代码,验证结果会放入Page.IsValid中,这样我们在后续的代码中就可以通过Page.IsValid来判断全部数据是否合法

        而如果我们想在Page_Load事件中就知道是否有非法数据,那就需要在Page_Load事件中先调用Page.Validate()进行检查。

        下面是一段简陋的代码,仅供参考:


    //Page.Validate执行前
    protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == true)
{
Page.Validate();
if (Page.IsValid == true)
{
//验证成功
                 DoSomeThing();
}
else
{
//验证失败
                return;
}
}
}

//Page.Validate已执行后
    protected void btnSave_Click(object sender, EventArgs e)
{
if (Page.IsValid == true)
{
//验证成功
             DoSomeThing();
}
else
{
//验证失败
            return;
}
}

 

from:http://toars.wordpress.com/2009/08/07/use-javascript-to-access-the-control-validators-in-asp-net-pages/
Use JavaScript to access the control Validators in ASP.NET pages
Sometimes, we want to access the Validators such as RequiredFieldValidator and CompareValidator in the client side and do something according to the user’s behavior. This can be done only use JavaScript.

Here is what I did in my application:

<script type=”text/javascript”>¨   function void myFunction(){
      if (typeof (Page_ClientValidate) == ‘function’){
         Page_ClientValidate();
      }
      if (Page_IsValid){
         // Do something
      }
      else{
           if (!Page_Validator[0].isvalid){
              // Do something
          }
      }
   }
</script>

来源:
录入日期:[2010/03/11 17:39:00]
收藏 推荐 打印 | 录入:mikebai | 阅读:
文章评论      
正在加载评论列表...
评论表单加载中...