.aspx file
<code>
<td class="label2" width="510"><strong>(Title):</strong> <asp:dropdownlist id="selConTitle" Runat="server" Width="140" Enabled="False" BackColor="#dcdcdc">
<asp:ListItem Value="" text="" Selected="True" />
<asp:ListItem Value="Title1" text="Title1" />
<asp:ListItem Value="Title2" text="Title2" />
<asp:ListItem Value="Title3" text="Title3" />
</asp:dropdownlist><asp:CustomValidator ID="custConTitle" Display="Dynamic" ControlToValidate="selConTitle" OnServerValidate="subConTitle"
Runat="server">*</asp:CustomValidator>
</td>
</code>
code behind in VB.NET
<code>
Public Sub subConTitle(ByVal objSource As Object, ByVal objArgs As ServerValidateEventArgs)
If Me.chkConOfficer.Checked Then
If Len(Me.selConTitle.SelectedValue) < 1 Then
Me.custConTitle.ErrorMessage = "You must select a title for the Officer of the Corporation."
objArgs.IsValid = False
Else
objArgs.IsValid = True
End If
End If
End Sub
</code>
For whatever reason the sub subConTitle is not being hit??
Anyone have any ideas ?
thanks,
-=angst=-It may be because your drop down list isn't enabled (even if you enable somewhere in code behind).
Try setting Enabled=True for the control by default and see if that works.
I tried that but no cigar! :(
Well, I haven't used validator controls before, so I'm not entirely sure.
One question however - when are you expecting the validation to takeplace, i.e. what event is causing the post-back? Do you have abutton that you use to submit the form or are you using some othermethod?
Thanks,
Tyler
By design, the CustomValidator does not get run when the control assigned to ControlToValidate has a blank value. Your first item in the list has a blank value. So expect it not to fire when set to that first item.
You can override this by NOT setting ControlToValidate and inside your evaluation function, directly use the listbox object.
Since you are trying to detect a blank item in the list box, you can actually use the RequiredFieldValidator. Just be sure that its InitialValue property is set to the same value as the blank item (which is "" in this case).
CustomValidator's OnServerValidate occurs only when
1. the control causing postback has 'CausesValidation' set to True.
2. if 'ControlToValidate' property of CustomValidator is set to control's name then its value must not be empty.
TylerFree: Yes a button is what submits the form.
PLBlum: your a genius. That indeed was the issue. I have other pages doing similar validation and it works because their values were set to something like 0 or 1. I just tossed a 0 in there and it works just fine!!! :)
Thank you so much. You just saved me a lot of time!
-=angst=-
p.s. I love this forum
p.p.s. "anger is a gift"
0 comments:
Post a Comment