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" />