CausesValidation=False but client side validation still occuring (controls generated in code behind)

StackOverflow https://stackoverflow.com/questions/20402583

Pergunta

I have an asp.net webform with static content (layout, fields, etc.) that creates a set of (image)buttons in code behind. I have required field validators and custom validators on my static content. Even though I'm setting CausesValidation on my new buttons, when clicked the page isn't posted back due to client validation issues.

The buttons are created as part of a sepearte button click event (i.e. not as part of the page load event).

    Private Sub CreateButtons_Click(sender As Object, e As EventArgs)
        Dim Btn As New ImageButton()
        Btn.ID = "Something"
        Btn.ImageUrl = "/Images/Something.png"
        Btn.CausesValidation = False
        AddHandler Btn.Click, AddressOf Btn_Click
        ControlToAddTo.Controls.Add(Btn)
    End Sub

    Private Sub Btn_Click(sender As Object, e As EventArgs)
        'Do something
    End Sub

I could turn off the client validation to solve this but I don't want to lose that functionality preventing unneccesary round trips to the server to validate. I've tried creating buttons in the page onload event and these work fine so I presume as part of the page lifecycle, client validation is initiated after the onload but before any element events are fired.

What's the best way to get CausesValidation=false to actually work...?


Edit: Example of the other aspx content on the page (fields and validators):

    Date of Birth: 
    <asp:RequiredFieldValidator ID="RFV_DOB" runat="server" ControlToValidate="DOB" Text=" *" ErrorMessage="Date of Birth is a required field" CssClass="Validation" ValidationGroup="Main" />
    <asp:CompareValidator ID="CV_DOB" runat="server" ControlToValidate="DOB" Operator="DataTypeCheck" Type="Date" Text=" *" ErrorMessage="DOB must be enterred in date format" CssClass="Validation" ValidationGroup="Main" />
    <asp:TextBox ID="DOB" runat="server" CssClass="ST_I" MaxLength="10" />

    Gender:
    <asp:RequiredFieldValidator ID="RFV_Gender" runat="server" ControlToValidate="Gender" Text=" *" ErrorMessage="Gender is a required field" CssClass="Validation" ValidationGroup="Main" />
    <asp:DropDownList ID="Gender" runat="server" CssClass="ST_I">
        <asp:ListItem Selected="True" />
        <asp:ListItem Text="Female" Value="F" />
        <asp:ListItem Text="Male" Value="M" />
        <asp:ListItem Text="Transgender" Value="T" />
        <asp:ListItem Text="Other" Value="O" />
    </asp:DropDownList>

    <asp:Button ID="Send" runat="server" Text="Send Message" style="margin: 20px;" ValidationGroup="Main" />
Foi útil?

Solução

Figured it out. I don't think it was client validation I was seeing either. I'd confused it for that because my server side onclick wasn't being reached. However the problem was actually with recreating my dynamic controls (I.e. the image buttons) on subsequent postbacks - in particular here, the postback initiated by the user clicking the button. It actually bypassed client validation but as I wasn't recreating the buttons on postback it didn't know that the button causing the postback was set to CausesValidation=false so server validation fired. It wasn't hitting the onclick because, again, it hadn't been recreated so didn't know the button causing the postback had an event handler.

Tip of the day, always recreate your dynamic controls on postback!

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top