日期:2014-05-17 浏览次数:20408 次
<form id="form1" runat="server"> <div> <asp:TextBox runat="server" ID="textBox1" ontextchanged="textBox1_TextChanged" ></asp:TextBox> <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="textBox1" ValidateEmptyText="true" ErrorMessage="CustomValidator" EnableClientScript="true" onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator> <input type="button" onclick="form1.submit();" value="TestSubmit" /> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="BulletList" ShowMessageBox="true" EnableClientScript="true"/> </div> </form>
public partial class TestValidate : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ValidationSummary1.HeaderText = "test"; ValidationSummary1.ValidationGroup += CustomValidator1.ID; } protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) { CustomValidator1.IsValid = textBox1.Text == "123"; Response.Write("验证了<br/>"); } protected void Button1_Click(object sender, EventArgs e) { Response.Write("Button<br/>"); CustomValidator1.IsValid = textBox1.Text == "123"; Response.Write("验证了<br/>"); } }