validation for checkboxlist and RadListBox inside radlistview asp.net

4 posts, 0 answers
  1. Rahul
    Rahul avatar
    39 posts
    Member since:
    Jan 2014

    Posted 15 Jan 2014 Link to this post

    Hi,
                 I want to validation on asp control Checkboxlist and Telrik control Radlistbox inside the radlist view using Required validator or custom validator. please provide sample code.

    Thanks,
    Rahul
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 16 Jan 2014 Link to this post

    Hi Rahul,

    Please have look into the sample code snippet which works fine at my end.

    ASPX:
    <telerik:RadListView ID="RadListView1" Width="97%" runat="server" DataSourceID="SqlDataSource1"
        ItemPlaceholderID="ProductsHolder">
        <LayoutTemplate>
            <fieldset style="max-width: 920px;" id="FieldSet1">
                <legend>Products</legend>
                <asp:Panel ID="ProductsHolder" runat="server" />
            </fieldset>
        </LayoutTemplate>
        <ItemTemplate>
            <asp:CheckBoxList ID="CheckBoxList1" runat="server">
                <asp:ListItem Text="Checked">
                </asp:ListItem>
                <asp:ListItem Text="Unchecked">
                </asp:ListItem>
            </asp:CheckBoxList>
            <asp:CustomValidator ID="CustomValidator2" runat="server" EnableClientScript="false"
                ErrorMessage="CustomValidator" OnServerValidate="CustomValidator2_ServerValidate">
            </asp:CustomValidator>
            <telerik:RadListBox runat="server" ID="RadListBox1">
                <Items>
                    <telerik:RadListBoxItem Text="Item1" />
                    <telerik:RadListBoxItem Text="Item2" />
                    <telerik:RadListBoxItem Text="Item3" />
                    <telerik:RadListBoxItem Text="Item4" />
                </Items>
            </telerik:RadListBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                ControlToValidate="RadListBox1">
            </asp:RequiredFieldValidator>
        </ItemTemplate>
    </telerik:RadListView>
    <telerik:RadButton ID="RadButton2" runat="server" Text="Validate">
    </telerik:RadButton>

    C#:
    protected void CustomValidator2_ServerValidate(object source, ServerValidateEventArgs args)
    {
        foreach (RadListViewItem item in RadListView1.Items)
        {
            CheckBoxList list = (CheckBoxList)item.FindControl("CheckBoxList1");
            for (int i = 0; i < list.Items.Count; i++)
            {
                if (list.Items[i].Selected == true)
                {
                    args.IsValid = true;
                    break;
                }
                else
                {
                    args.IsValid = false;
                }
            }
        }
    }

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Rahul
    Rahul avatar
    39 posts
    Member since:
    Jan 2014

    Posted 16 Jan 2014 Link to this post

    HI Shinu,
               Thanks for quick reply, I want to do this client side validation using required field validatior at least one checkbox must be selected and my design code as follows.
        <tr>
                                                <td colspan="2">
                                                    <div style="border:solid 1px #EEEEEEwidth:100%;">
                                                       <div style="border:1px solid gray;" id = "dvListView">
                                                            <telerik:RadListView runat="server"  ID="RadListView1" onitemdatabound="RadListView1_ItemDataBound" BorderColor="White" BorderStyle="None" AllowPaging="True">
                                                                <ClientSettings></ClientSettings>                                                       
               
                                                                <ItemTemplate>
                                                                   <div>
                                                                   <table>
                                                                         <tr>
                                                                            <td class="LabelWidth">
                                                                                <asp:Label ID="Label4" runat="server" Text='<%# (DataBinder.Eval(Container.DataItem,"PermissionItemName")) %>'></asp:Label>
                                                                            </td>                                                                        
                                                                            <td>
                                                                                <asp:CheckBoxList  ID="Listbox_Permission" runat="server" RepeatDirection="Horizontal" CssClass="horizontalListbox">
                                                                                </asp:CheckBoxList>         
                                                                              
                                                                              
                                                                            </td>
                                                                        </tr>                                         
                                                                  </table>
                                                                 </div>
                                                              </ItemTemplate>
                                                         </telerik:RadListView>
                                                         <telerik:RadListBox ID="qw" runat="server"  BorderColor="White" BorderStyle="None" DataValueField="PermissionItemOpearationId" 
                                                           DataTextField="PermissionOperationName"  BorderWidth="0" AutoPostBack="false" CssClass="horizontalListbox"></telerik:RadListBox> 
                                                           <%-- <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="cb_selectone">Please check atleast one checkbox.</asp:CustomValidator>--%>
                                                     </div>
                                                  </div>
                                                </td>
                                            </tr>
     <asp:Button ID="btn_new" CssClass="btn btn-sm btn-success" runat="server" Text="Submit"  onclick="btn_new_Click" ValidationGroup="role"  />
    


  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 16 Jan 2014 Link to this post

    Hi Rahul,

    I guess you want to show a RequierdFieldValidator for ASP CheckBoxList. But there is no field in the CheckBoxList to fill, so RequierdFieldValidator doesn't work for this Control. So you have to validate ASP CheckBoxList by CustomValidator. Please elaborate your requirement if it doesn't help.

    Thanks,
    Shinu.
Back to Top