0

aspx 代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MyWebForm.aspx.cs" Inherits="MyWebApplication.MyWebForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body style="height: 334px">
    <form id="form1" runat="server">
    <div style="height: 338px">

        <asp:TextBox ID="MyTextBox" runat="server" AutoPostBack="True"></asp:TextBox>
        <br />
        <asp:CustomValidator ID="MyCustomValidator" runat="server" 
            ControlToValidate="MyTextBox" ErrorMessage="My error message" 
            onservervalidate="Foo2"></asp:CustomValidator>

    </div>
    </form>
</body>
</html>

C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyWebApplication
{
    public partial class MyWebForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        bool Foo1(string bar)
        {
            if (bar == "bar")
                return true;
            return false;
        }

        protected void Foo2(object source, ServerValidateEventArgs args)
        {
            args.IsValid = this.Foo1(this.MyTextBox.Text);
        } // breakpoint
    }
}

我在 Foo2 函数(注释)中设置了断点,但调试器没有访问它。我试着写不同的文本,不写任何东西,写“栏”,然后按回车和制表符,但它仍然不起作用。我的意思是文本更改操作。是否可以?

4

1 回答 1

3

没有提交按钮。您需要添加一个按钮来提交表单,然后触发页面验证。

<asp:Button runat="server" id="myButton" Text="Submit" />
于 2013-01-26T10:39:22.520 回答