This is a migrated thread and some comments may be shown as answers.

Nested controls validation

1 Answer 34 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Gary
Top achievements
Rank 1
Gary asked on 04 Oct 2008, 10:38 AM
Hi,
I have a detailsview with a radcombobox in the emptytemplate.  I want to validate this (I am using load on demand).  It seems I need to validate via javascript, but can not get this to working with examples you gave in other forums.  I want to prevent the users from typing in an incorrect code in the combo box - one that does not exist and pass validation.  Here's what I tried:
        function validateCombo(source, args)    
        {    
           args.IsValid = false;  
           var elements = document.getElementsByTagName('*');  
           var count = elements.length;  
           var targetId = 'rcbCAGE';  
           var i;  
           for (i = 0; i < count; i++) {  
                var element = elements[i];  
                var pos = element.id.indexOf(targetId);  
                if(pos >= 0 && element.type == 'text' ) {  
                    //found rad combo element  
                   var text = element.get_text();    
                   var item = element.findItemByText(text);    
                   if (item)    
                   {    
                       args.IsValid = true;    
                   }    
                }  
        } 

The line var text = element.get_test() throws an error that the method is not valid.

Here's a snippet of the HTML page:

        <asp:DetailsView ID="dvCageCode" DataKeyNames="CageCodeID" runat="server" AutoGenerateRows="False" 
            DataSourceID="CAGEData" Height="50px" Width="337px"   
            OnItemCommand="dvCageCode_ItemCommand"   
            OnItemCreated="dvCageCode_ItemCreated"   
            ondatabound="dvCageCode_DataBound"   
            onitemupdating="dvCageCode_ItemUpdating"   
            oniteminserting="dvCageCode_ItemInserting"   
            OnItemInserted="OnItemInserted" 
            OnItemUpdated="OnItemUpdated" 
            CellPadding="10" CommandRowStyle-Height="50px"   
                CommandRowStyle-VerticalAlign="Middle">  
              
<CommandRowStyle VerticalAlign="Middle" Height="50px"></CommandRowStyle> 
              
            <EmptyDataTemplate> 
                <table> 
                    <tr> 
                        <td nowrap="nowrap">Cage Code:</td> 
                        <td nowrap="nowrap">  
                            <telerik:RadComboBox ID="rcbCAGE" runat="server" EnableLoadOnDemand="true" MarkFirstMatch="false" 
                                    OnItemsRequested="rcb_ItemsRequested" OnSelectedIndexChanged="rcb_SelectedIndexChanged" AutoPostBack="true" ShowMoreResultsBox="true"  Skin="Mac" /> 
                            <asp:Label ID="lblCageRequired" runat="server" Text="*" CssClass="RequiredMark" Visible="true" /> 
                            <asp:RequiredFieldValidator ID="rValCageCode" runat="server" Text="Please select a CAGE Code" InitialValue="" ControlToValidate="rcbCAGE" ErrorMessage="*" Enabled="true" SetFocusOnError="true" ValidationGroup="Submit" Display="Dynamic" /> 
                            <asp:CustomValidator ID="cValCAGE"     
                                runat="server"     
                                ControlToValidate="rcbCAGE"    
                                ValidateEmptyText="True"    
                                ClientValidationFunction="validateCombo"    
                                ErrorMessage="Please select a CAGE Code from the list">    
                            </asp:CustomValidator>    
 
                        </td> 
                    </tr> 
                </table> 
            </EmptyDataTemplate> 
 

Thanks in advance for your help.
Gary

1 Answer, 1 is accepted

Sort by
0
Rosi
Telerik team
answered on 06 Oct 2008, 06:57 AM
Hello Gary,

I suggest you use Telerik.Web.UI.RadComboBox.ComboBoxes array to access the comboboxes and edit your code by the following way:

function validateCombo(source, args)       
        {       
           args.IsValid = false;     
   
           var i;     
           for (i = 0; i < Telerik.Web.UI.RadComboBox.ComboBoxes.length; i++) {     
                var combo= Telerik.Web.UI.RadComboBox.ComboBoxes[i];  
                var pos = element.id.indexOf(targetId);     
                                    //found rad combo element     
                   var text = combo.get_element().get_text();       
                   var item = combo.findItemByText(text);       
                   if (item)       
                   {       
                       args.IsValid = true;       
                   }       
                     
        }    
 

Regards,
Rosi
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
ComboBox
Asked by
Gary
Top achievements
Rank 1
Answers by
Rosi
Telerik team
Share this question
or