开发者

Custom Validator not working but allows post back instead

        <div>
            <asp:Label ID="lblClientId" runat="server" CssClass="label" meta:resourcekey="lblClientIdResource" /> 
            <asp:TextBox ID="tbClientId" runat="server"  style="width:150px; "/>
            <asp:Button ID="btnClientId" runat="server"  style="width:50px;" meta:resourcekey="btnClientIdRe开发者_JAVA百科source" />
            <asp:CustomValidator ID="rfvClientId" runat="server" ValidationGroup="ClientId" meta:resourcekey="rfvClientIdResource" ControlToValidate="tbClientId" ClientValidationFunction="BtnClickClientId" style="position:absolute;"  ValidateEmptyText="True" ><asp:Image ID="Image2" ImageUrl="caution_20.png" runat="server" /></asp:CustomValidator>
       </div>



<script type="text/javascript">
        function BtnClickClientId(session, args) {
            ButtonClick(session, args, "<%= tbClientId.ClientID %>", "<%= lblClientId.ClientID %>");
        }
        window.onload = function () {
            document.getElementById('<%= tbClientId.ClientID%>').focus();
        };
    </script>


<asp:ValidationSummary ID="ClientIdValidationSummary" runat="server" BackColor="LightGray" DisplayMode="BulletList" CssClass="validationSummary" EnableClientScript="true" HeaderText='<%$ Resources:GlobalResource, ValidationSummaryResource %>'/>

So this ButtonClick() method is working and has been tested independently. The problem is that when i enter nothing into the text box and click the button, the validator works as expected and appears on the screen. Then it disappears. It is also never shown in the page validation summary. How do I get this to work?

I have tried to also set a required field validator on this text box and it seems to work with that but I do not want to use two validators.

     <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tbClientId" ErrorMessage="RequiredFieldValidator" style="position:absolute;"><asp:Image ID="Image2" ImageUrl="caution_20.png" runat="server" /></asp:RequiredFieldValidator>
            <asp:CustomValidator ID="rfvClientId" runat="server" ValidationGroup="ClientId" meta:resourcekey="rfvClientIdResource" ControlToValidate="tbClientId" ClientValidationFunction="BtnClickClientId" style="position:absolute;"  ValidateEmptyText="True" ></asp:CustomValidator>

This code works but I should not have to use 2 validators.


You need to set the "arg.IsValid" to "true" or "false" in the javascript function based on your requirement (i.e. to "true" when you think the validation is successful and false otherwise). Also, in the code behind file, it is always advisable to check for "Page.IsValid" property inside the click event handler of the button. So, in the javascript add this.

  arg.IsValid = false;

and in code behind

  protected void button_click(..)
  {
       if (Page.IsValid)
       {
           // Your code, if any exists
       }
  }

Hope this helps!!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜