ComboBox wrapped in Server Control causes validation when it shouldn't

8 posts, 1 answers
  1. iTools
    iTools avatar
    28 posts
    Member since:
    Jun 2007

    Posted 20 Jul 2010 Link to this post

    Even though it makes the title of this thread invalid, I have simplified the problem in the next post...
  2. iTools
    iTools avatar
    28 posts
    Member since:
    Jun 2007

    Posted 20 Jul 2010 Link to this post

    Even given a simpler scenario I get unexpected results.

    <asp:ScriptManager ID="ctlScriptManager" runat="server" />
    <telerik:RadComboBox ID="ddlCombo" runat="server" AllowCustomText="true">
        <Items>
            <telerik:RadComboBoxItem Text="Item 1" Value="1" />
            <telerik:RadComboBoxItem Text="Item 2" Value="2" />
            <telerik:RadComboBoxItem Text="Item 3" Value="3" />
        </Items>
    </telerik:RadComboBox>
    <asp:CustomValidator ID="ctlValidator" runat="server" ControlToValidate="ddlCombo"
        ErrorMessage="Please select a predefined item" />
    <br />
    <asp:Button ID="btnCausePostBackWithoutValidation" runat="server" Text="Post Back w/o Validation"
        CausesValidation="false" />

    Private Sub ctlValidator_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs) Handles ctlValidator.ServerValidate
        Dim selectedID As Integer
        If Not Integer.TryParse(ddlCombo.SelectedValue, selectedID) Then
            args.IsValid = False
        Else
            args.IsValid = selectedID > 0
        End If
    End Sub

    In this scenario I would never expect the validator to be called. Yet, if text is typed in the combo and the button is clicked the validator is called. The only time the validator is not called, as expected, is when no custom text is typed in the ComboBox.

    Please advise.
    Thanks.
  3. Kalina
    Admin
    Kalina avatar
    918 posts

    Posted 26 Jul 2010 Link to this post

    Hello jan fischer,

    The issue that you describe is very strange.
    I used your code to create a test page. After running the page and testing it with our latest version (Telerik.Web.UI 2010.2.713.20) under Internet Explorer and Firefox, I can say that the CustomValidator control never fires on my side.
    Please find the sample attached and give it a try. Maybe I miss something?

    Greetings,
    Kalina
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  4. iTools
    iTools avatar
    28 posts
    Member since:
    Jun 2007

    Posted 26 Jul 2010 Link to this post

    Hello Kalina,

    Your sample is flawed, as the validator is not hooked-up to the ServerValidate event handler.

    After changing the ServerValidator event handler to ...
    Private Sub ctlValidator_ServerValidate(ByVal source As Object, ByVal args As ServerValidateEventArgs) Handles ctlValidator.ServerValidate
        Dim selectedID As Integer = 0
        If Not Integer.TryParse(ddlCombo.SelectedValue, selectedID) Then
            args.IsValid = False
        Else
            args.IsValid = selectedID > 0
        End If
    End Sub
    ... the unexpected validation behaviour is still observed. I am using the latest Telerik.Web.UI for testing purposes.

    Cheers
  5. Answer
    Kalina
    Admin
    Kalina avatar
    918 posts

    Posted 30 Jul 2010 Link to this post

    Hi jan fischer,

    Please excuse my omission.
    I reproduced the issue and it is really strange.
    Let me suggest you set the ValidationGroup property of the RadComboBox and Button :
    <telerik:RadComboBox ID="ddlCombo" runat="server"
        AllowCustomText="true" ValidationGroup="test">
        <Items>
            <telerik:RadComboBoxItem Text="Item 1" Value="1" />
            <telerik:RadComboBoxItem Text="Item 2" Value="2" />
            <telerik:RadComboBoxItem Text="Item 3" Value="3" />
        </Items>
    </telerik:RadComboBox>
    <asp:CustomValidator ID="ctlValidator" runat="server"
        ControlToValidate="ddlCombo"
        ErrorMessage="Please select a predefined item" />
    <br />
    <asp:Button ID="btnCausePostBackWithoutValidation" runat="server"
        Text="Post Back w/o Validation"
        CausesValidation="false" ValidationGroup="test"
        OnClick="btnCausePostBackWithoutValidation_OnClick" />
        <asp:Label ID="lblTest" runat="server"></asp:Label>

    If the issue persists - could you please explain in more details the scenario that you are trying to implement?

    Best wishes,
    Kalina
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  6. iTools
    iTools avatar
    28 posts
    Member since:
    Jun 2007

    Posted 01 Aug 2010 Link to this post

    Hello Kalina,

    That does seem to circumvent the unexpected behaviour, however it seems like a bug?
    Now, I stuck another button on the form, one that should cause validation.

    <asp:ScriptManager ID="ctlScriptManager" runat="server" />
    <telerik:radcombobox id="ddlCombo" runat="server" allowcustomtext="true" emptymessage="Please Select"
        onclientdropdownopening="OnClientDropDownOpening" enableloadondemand="true" validationgroup="test" />
    <asp:CustomValidator ID="ctlValidator" runat="server" ControlToValidate="ddlCombo"
        ErrorMessage="Please select a predefined item" />
    <br />
    <asp:Button ID="btnCausePostBackWithoutValidation" runat="server" Text="Post Back w/o Validation"
        CausesValidation="false" ValidationGroup="test" />
    <asp:Button ID="btnCausePostBackWithValidation" runat="server" Text="Validate" />

    It would seem that expected behaviour would mean the validation handler is called when the btnCausePostBackWithValidation button was pressed without changing the RadCombo control, but the validator is not called. Only a change of the RadCombo causes validator to fire...

    Cheers,
    Jan
  7. T. Tsonev
    Admin
    T. Tsonev avatar
    2817 posts

    Posted 05 Aug 2010 Link to this post

    Hello,

    I believe this is the expected behavior in this case - the CustomValidator won't fire when the control to validate is empty. To quote MSDN - "If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to prevent the user from skipping an input control."

    Alternatively set ValidateEmptyText="true"

    I hope this helps.

    Kind regards,
    Tsvetomir Tsonev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  8. iTools
    iTools avatar
    28 posts
    Member since:
    Jun 2007

    Posted 05 Aug 2010 Link to this post

    Hey Tsvetomir,

    Very right, I should be using a RequiredFieldValidator for that, my bad :)
    The workaround for setting the validationgroup on the controls will be an adequate one for my circumstance.

    Thanks,
    Jan
Back to Top