.net 流氓
天下无难事,有志者成之;天下无易事,有恒者得之。
1 <%@ Page Language="C#" %>
2
3 <script runat="server">
4
5 protected void Button1_Click(Object sender, EventArgs e) {
6 Label1.Text = "VALID NUMBER!";
7 }
8
9 </script>
10
11 <html xmlns="http://www.w3.org/1999/xhtml">
12 <head id="Head1" runat="server">
13 <title>CustomValidator</title>
14
15 <script type="text/javascript">
16 function validateNumber(oSrc, args) {
17 args.IsValid = (args.Value % 5 == 0);
18 }
19 </script>
20
21 </head>
22 <body>
23 <form id="form1" runat="server">
24 <div>
25 <p>
26 Number:
27 <asp:TextBox ID="TextBox1"
28 Runat="server"></asp:TextBox>
29 &nbsp;
30 <asp:CustomValidator ID="CustomValidator1" Runat="server" ControlToValidate="TextBox1" Text="Number must be divisible by 5"
31 ClientValidationFunction="validateNumber">
32 </asp:CustomValidator>
33 </p>
34 <p>
35 <asp:Button ID="Button1" OnClick="Button1_Click" Runat="server" Text="Button"></asp:Button>
36 </p>
37 <p>
38 <asp:Label ID="Label1" Runat="server"></asp:Label>
39 </p>
40 </div>
41 </form>
42 </body>
43 </html>

1 <%@ Page Language="C#" %>
2
3 <script runat="server">
4
5 protected void Button1_Click(Object sender, EventArgs e) {
6 if (Page.IsValid) {
7 Label1.Text = "VALID ENTRY!";
8 }
9 }
10
11 void ValidateNumber(object source, ServerValidateEventArgs args)
12 {
13 try
14 {
15 int num = int.Parse(args.Value);
16 args.IsValid = ((num%5) == 0);
17 }
18 catch(Exception ex)
19 {
20 args.IsValid = false;
21 }
22 }
23
24 </script>
25
26 <html xmlns="http://www.w3.org/1999/xhtml" >
27 <head id="Head1" runat="server">
28 <title>CustomValidator</title>
29 </head>
30 <body>
31 <form id="form1" runat="server">
32 <div>
33 <p>
34 Number:
35 <asp:TextBox ID="TextBox1"
36 Runat="server"></asp:TextBox>
37 &nbsp;
38 <asp:CustomValidator ID="CustomValidator1"
39 Runat="server" ControlToValidate="TextBox1"
40 Text="Number must be divisible by 5"
41 OnServerValidate="ValidateNumber"></asp:CustomValidator>
42 </p>
43 <p>
44 <asp:Button ID="Button1" OnClick="Button1_Click"
45 Runat="server" Text="Button"></asp:Button>
46 </p>
47 <p>
48 <asp:Label ID="Label1" Runat="server"></asp:Label>
49 </p>
50 </div>
51 </form>
52 </body>
53 </html>

1 <asp:CustomValidator ID="CustomValidator1"
2 Runat="server" ControlToValidate="TextBox1"
3 Text="Number must be divisible by 5"
4 ClientValidationFunction="validateNumber"
5 OnServerValidate="ValidateNumber"></asp:CustomValidator>
posted on 2011-06-14 07:05  .net 流氓  阅读(425)  评论(0编辑  收藏  举报