Custom validator in <ASP:Table>

Discussion in 'ASP .Net' started by Andy, Oct 25, 2006.

  1. Andy

    Andy Guest

    Hi folks,

    I have a customvalidator control that works properly if it isn't
    contained in an ASP:TABLE. But, when I place it inside an ASP:TABLE, I
    find that _ServerValidate doesn't get fired, even if I force the
    customvalidator to validate by invoking its validate() method after a
    button is clicked:

    cvCustomValidator_ServerValidate(object source,
    system.Web.UI.WebControls.ServerValidateEventArgs args) <== doesn't get
    called

    <asp:table id="tblTable" runat="server" Width="100%">
    <asp:TableRow>
    <asp:TableCell>
    <asp:TextBox runat="server" Width="400px" Font-Names="Verdana"
    ID="txt_textbox"></asp:TextBox>
    <asp:CustomValidator runat="server" ControlToValidate="txt_textbox"
    ErrorMessage="This is an error"
    ID="cvCustomValidator" Visible="true" Display="Static"
    EnableClientScript="false" Enabled="true"
    EnableViewState="true">*</asp:CustomValidator>
    </asp:TableCell>
    </asp:TableRow>
    </asp:table>

    I don't have any client-side validation set for the validator, and when
    I run a test, I key characters into the textbox. I know the
    servervalidate method isn't being called because I write to a log from
    inside ServerValidate when its called.

    Does anyone know what's going wrong?

    Is there some parameter I have to set to enable the validator to work
    from inside an ASP:TABLE?

    Andy
     
    Andy, Oct 25, 2006
    #1
    1. Advertising

  2. Andy

    Andy Guest

    After some reading and testing, it seems that the custom validator
    doesn't fire an event to trigger a server-side validation routine after
    the form is posted when there is no client-side validation routine
    assigned to the custom validator in its ClientValidationFunction
    property, regardless if the need for such a function is actually
    specified by the validator's enableClientScript property.

    When I assign a dummy function to the page's client-side jscript, such
    as:

    function customvalidator(source,args){
    args.IsValid=true;
    }

    the server-side validation routine gets invoked properly after the form
    is posted.

    Validators can be contained in ASP:TABLEs, as long as the control being
    validated is inside the same tablecell that the validator is in. The
    same holds true for any other web control that can contain a validator
    (such as a panel) - the field being validated HAS to be in the same
    container as the validator.

    Andy
     
    Andy, Oct 27, 2006
    #2
    1. Advertising

  3. Andy

    Andy Guest

    Hey gang,

    If you find your server-side customValidation handler STILL isn't being
    called after you've set your client-side validiation routine, try this:

    Set the OnServerValidate attribute of your custom validator to your
    validating function's name. This can be done on your webpage as:

    <asp:customvalidator id="cvEmailAddress" runat="server"
    OnServerValidate="emailcheck">*</asp:customvalidator>

    or in your codebehind in your Page Load (I've used C#) as:
    tb_ApplicantEmail.Attributes.Add("OnServerValidate","emailcheck");


    Then, in your codebehind, make sure that your validating function is
    externally accessible (I've made mine protected - NEVER use public for
    this):

    protected void emailcheck(object source,
    System.Web.UI.WebControls.ServerValidateEventArgs args)
    {
    u.sendReport("emailcheck"); //this is my logging routine
    bool blnResult=false;

    args.IsValid=blnResult; //read via Page.IsValid
    }


    Andy
     
    Andy, Oct 27, 2006
    #3
  4. Andy

    Andy Guest

    The custom validator seems to be one of those components in .NET that
    exposes several ways to do the same thing, and this may result in
    problems if you try to mix them.

    If after following the previous posts you find now that your custom
    validator is firing too many times (ie twice for each user event), its
    probably because you are now calling it twice.

    How?

    Well, the custom validator CAN fire on its own, however, Visual
    Studio's Designer has to attach your validation method to the custom
    validator control. It does this by writing out a line in the
    InitializeComponent() method of your program:

    private void InitializeComponent(){
    this.cvEmailAddress.ServerValidate += new
    System.Web.UI.WebControls.ServerValidateEventHandler(this.emailcheck);
    }

    If you cut and paste controls or rename validation handlers in your
    source code, the designer may not have written this line out
    immeadiately (or the existing line contains a reference to an obsolete
    validation method). This results in what looks like an unresponsive
    custom validator control (the previous posts remedy this by adding the
    event handler to the HTML code of your webpage).

    But, there maybe a situation later on in your visual studio editing
    sessions that causes the Visual Studio designer to properly write out
    (or update) the line that attaches your validation method to the custom
    validator event handler. When this happens, you end up with two places
    where your validation code is called.

    The first is from the OnServerValidate event handler in your HTML page
    described earlier in this chain. The second is from the new event
    handler linkage the Visual Studio Designer just created. When this
    happened to me, I found that I liked the InitializeComponent linkage
    better so I dropped the OnServerValidate reference in my HTML page.

    Hope this helps somebody. Andy
     
    Andy, Oct 30, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    9,799
    ganniss
    Jan 19, 2006
  2. Jay Douglas
    Replies:
    0
    Views:
    542
    Jay Douglas
    Feb 27, 2007
  3. Replies:
    4
    Views:
    936
    Richter~9.6
    Feb 13, 2007
  4. Lewis Holmes
    Replies:
    5
    Views:
    2,808
    Allen Chen [MSFT]
    Mar 18, 2009
  5. Scott Yenor

    How to get custom control to work with custom validator

    Scott Yenor, Jan 27, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    227
    Scott Yenor
    Jan 27, 2004
Loading...

Share This Page