日期:2014-05-17  浏览次数:20408 次

【求解】为何直接提交表单不显示验证消息,而ASP Button单击事件处理方法中的验证却可以显示验证消息
问题描述:
页面中共有4个控件(textBox1,CustomValidator1,input button单击提交,asp button 单击验证)
后台有3个方法(Pageload,CustomValidator1_ServerValidate,Button1_Click)
CustomValidator1_ServerValidate和Button1_Click的处理内容相近;
操作流程:
首先,在textbox1中输入错误的内容,然后分别执行:
单击input button提交表单后,页面内容中不显示 验证错误消息;
单击asp button后,页面内容显示验证错误消息;
Form:
XML code
    
<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>


Cs:
C# code

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/>");
        }
    }



------解决方案--------------------
[code=C#][/code] <asp:Button ID="bt_operateItem" runat="server" Text="aa" ValidationGroup="item"
CssClass="confirm" OnClick="bt_operateItem_Click" Style="height: 22px; line-height: 20px;" />
<asp:ValidationSummary ID="ValidationSummary1" ValidationGroup="item" ShowMessageBox="true"
ShowSummary="false" runat="server" />