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

[Solved] Grid Validation based on dropdownlist selection

6 Answers 308 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 30 Jun 2009, 01:23 PM
I have a RadGrid which has several column that are asp:DropDownList inside and itemtemplate.  One of these dropdownlist has a selection that if selected needs to validate a RadDatePicker.  All I need to do is have the RadDatePicker required if the dropdowlist has a certain value selected. 

Listed below, if someone clicks on "Weekly-PM"  I need to require the RadDateThru.  Thanks for your help.

<telerik:GridTemplateColumn HeaderText="Time" Groupable="false">  
   <ItemTemplate> 
       <asp:DropDownList ID="DropDownListTime" AutoPostBack="true" runat="server">  
            <asp:ListItem Text="Not Set" Value="0" /> 
            <asp:ListItem Text="Morning" Value="1" /> 
            <asp:ListItem Text="Evening" Value="2" /> 
            <asp:ListItem Text="Both" Value="3" /> 
            <asp:ListItem Text="Now" Value="4" /> 
            <asp:ListItem Text="Weekly-PM" Value="5" /> 
       </asp:DropDownList> 
       <asp:DropDownList ID="DropDownListDays" SelectionMode="Single" Enabled="false" runat="server">  
             <asp:ListItem Text="M" Value="2" /> 
             <asp:ListItem Text="T" Value="3" /> 
             <asp:ListItem Text="W" Value="4" /> 
             <asp:ListItem Text="R" Value="5" /> 
             <asp:ListItem Text="F" Value="6" /> 
       </asp:DropDownList> 
     </ItemTemplate> 
  </telerik:GridTemplateColumn> 
  <telerik:GridTemplateColumn HeaderText="Date" Groupable="false">  
      <ItemTemplate> 
         <telerik:RadDatePicker ID="RadDateThru" MinDate="2000-2-1" runat="server">  
           <DateInput ID="DateInput1" runat="server" />                                  </telerik:RadDatePicker>                                
   </ItemTemplate> 
</telerik:GridTemplateColumn> 

6 Answers, 1 is accepted

Sort by
0
BaiH
Top achievements
Rank 1
answered on 30 Jun 2009, 06:10 PM
I think a custom validator will match exactly the scenario you have described. You should set it to validate the DataPicker and in its validate event you can check the selection of the dropdown and set  the appropriate validator's state.

--BH
0
Mike
Top achievements
Rank 1
answered on 30 Jun 2009, 07:30 PM
I'm not sure that would work.
0
BaiH
Top achievements
Rank 1
answered on 30 Jun 2009, 08:28 PM
Huh.. Strange it seems to work on my side.. but who knows, maybe I'm missing something ;)

<script type="text/javascript"
 
            function clientValidationFunction(sender, args) { 
                var currentCell = sender.parentNode; 
                var currentRow = currentCell.parentNode 
                var dropdown = $telerik.findElement(currentRow, "DropDownListTime"); 
                var datePicker = $telerik.findControl(currentCell, "RadDateThru"); 
                 
                if (dropdown && dropdown.selectedIndex == 5 &&  
                    (datePicker.get_selectedDate() == null || datePicker.get_selectedDate() == "")) 
                    args.IsValid = false
            } 
        </script> 

<telerik:GridTemplateColumn HeaderText="Time" Groupable="false"
                        <ItemTemplate> 
                            <asp:DropDownList ID="DropDownListTime" AutoPostBack="true" runat="server"
                                <asp:ListItem Text="Not Set" Value="0" /> 
                                <asp:ListItem Text="Morning" Value="1" /> 
                                <asp:ListItem Text="Evening" Value="2" /> 
                                <asp:ListItem Text="Both" Value="3" /> 
                                <asp:ListItem Text="Now" Value="4" /> 
                                <asp:ListItem Text="Weekly-PM" Value="5" /> 
                            </asp:DropDownList> 
                            <asp:DropDownList ID="DropDownListDays" SelectionMode="Single" Enabled="false" runat="server"
                                <asp:ListItem Text="M" Value="2" /> 
                                <asp:ListItem Text="T" Value="3" /> 
                                <asp:ListItem Text="W" Value="4" /> 
                                <asp:ListItem Text="R" Value="5" /> 
                                <asp:ListItem Text="F" Value="6" /> 
                            </asp:DropDownList> 
                        </ItemTemplate> 
                    </telerik:GridTemplateColumn> 
                    <telerik:GridTemplateColumn HeaderText="Date" Groupable="false"
                        <ItemTemplate> 
                            <telerik:RadDatePicker ID="RadDateThru" MinDate="2000-2-1" runat="server"
                                <DateInput ID="DateInput1" runat="server" /> 
                            </telerik:RadDatePicker> 
                            <asp:CustomValidator runat="server" ID="customValidator" ControlToValidate="RadDateThru" 
                                ClientValidationFunction="clientValidationFunction" ValidateEmptyText="true" /> 
                        </ItemTemplate> 
                    </telerik:GridTemplateColumn> 

And don't forget to implement the same validation logic server-side too.

-- BH
0
Mike
Top achievements
Rank 1
answered on 30 Jun 2009, 11:33 PM
I appreciate your help on this, but I added your solution and it still submits the items in the grid.
0
BaiH
Top achievements
Rank 1
answered on 01 Jul 2009, 04:52 PM
Can you verify that you have set Validator's ValidateEmptyText property to true as shown in the previous code snippet.

--BH
0
Mike
Top achievements
Rank 1
answered on 01 Jul 2009, 06:10 PM
I verifed that it was set to true.  Also, I ended up using server side validation and it works pretty good.
Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Answers by
BaiH
Top achievements
Rank 1
Mike
Top achievements
Rank 1
Share this question
or