Loading RadListBox with preselected items linked with RequiredFieldValidator

3 posts, 0 answers
  1. Datamex
    Datamex avatar
    161 posts
    Member since:
    Jun 2012

    Posted 18 May 2011 Link to this post

    Hi,

    I've got a RadListBox linked with a RequiredFieldValidator from ASP.NET and it's possible this RadListBox loads with preselected items. So, I open the corresponding page and reckon I haven't got to make a change in the selection of the RadListBox and just press my validation button. Unfortunately, I'm getting an error from the validator that I haven't selected a thing in my RadListBox, while there are some items preselected.

    My RadListBox looks like:
    <telerik:RadListBox ID="DepartmentsListBox" runat="server" CheckBoxes="true" SelectionMode="Multiple" Width="95%" DataSourceID="DepartmentsDataSource" DataTextField="Name" DataValueField="DepartmentID" OnDataBound="DepartmentsListBox_DataBound">
    </telerik:RadListBox>
    <asp:ObjectDataSource ID="DepartmentsDataSource" runat="server"
        SelectMethod="GetDepartments" TypeName="Datamex.Projects.Nebulus.BusinessLogics.Departments">
    </asp:ObjectDataSource>

    The RadListBox loads like:
    protected void DepartmentsListBox_DataBound(object sender, EventArgs e)
    {
        foreach (RadListBoxItem item in DepartmentsListBox.Items)
        {
            item.Checked = hc.DepartmentHourCategoryLinks.Any(l => l.DepartmentID == Guid.Parse(item.Value));
        }
    }

    And the validator:
    <asp:RequiredFieldValidator ID="DepartmentsValidator" runat="server" ControlToValidate="DepartmentsListBox" ValidationGroup="Save" CssClass="Validator" Text="*" ErrorMessage="<%$ Resources:Resources, DepartmentsRequiredMessage %>" />

    As you can see the DataBound event checks the preselected items as it should, but if I click my validation button I get the error from my validator that I haven't checked any items in the RadListBox.

    What am I doing wrong?

    Thanks in advance.

    Regards,
    Datamex
  2. Datamex
    Datamex avatar
    161 posts
    Member since:
    Jun 2012

    Posted 19 May 2011 Link to this post

    Hi there,

    I already solved my problem. For other people struggling with this, here's what I did to solve the issue.

    Instead of using a RequiredFieldValidator I used a CustomValidator and remember to not set the ControlToValidate property.

    .aspx of my RadListBox:
    <telerik:RadListBox ID="DepartmentsListBox" runat="server" CheckBoxes="true" SelectionMode="Multiple" Width="95%" DataSourceID="DepartmentsDataSource" DataTextField="Name"
      DataValueField="DepartmentID" OnDataBound="DepartmentsListBox_DataBound">
    </telerik:RadListBox>
    <asp:ObjectDataSource ID="DepartmentsDataSource" runat="server"
      SelectMethod="GetDepartments" TypeName="Datamex.Projects.Nebulus.BusinessLogics.Departments">
    </asp:ObjectDataSource>
    <asp:CustomValidator ID="DepartmentsValidator" runat="server" Text="*" CssClass="Validator" ClientValidationFunction="ValidateDepartmentsListBox" OnServerValidate="DepartmentsValidator_ServerValidate" ErrorMessage="<%$ Resources:Resources, DepartmentsRequiredMessage %>" ValidationGroup="Save" />

    As you can see my CustomValidator has both client-side and server-side validation.

    Client-side validation (javascript):
    function ValidateDepartmentsListBox(sender, args) {
      var listbox = $find('<%= DepartmentsListBox.ClientID %>');
      args.IsValid = listbox.get_checkedItems().length > 0;
    }

    Server-side validation (code-behind; C#):
    protected void DepartmentsValidator_ServerValidate(object source, ServerValidateEventArgs args)
    {
      args.IsValid = this.DepartmentsListBox.CheckedItems.Count > 0;
    }

    With these modifications the RadListBox object works as expected.

    HTH others who struggle with this.

    Regards,
    Datamex
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 19 May 2011 Link to this post

    Hello Datamex,

    This behavior is expected since not selecting any items. You need to set the selected property to true along with checked property.
    C#:
    protected void RadListBox3_ItemDataBound(object sender, RadListBoxItemEventArgs e)
       {
           foreach (RadListBoxItem item in RadListBox3.Items)
           {
              item.Checked = true;
              item.Selected = true;
           }
       }

    Thanks,
    Shinu.
Back to Top