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

Loading RadListBox with preselected items linked with RequiredFieldValidator

2 Answers 161 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Datamex
Top achievements
Rank 2
Datamex asked on 18 May 2011, 04:47 PM
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 Answers, 1 is accepted

Sort by
0
Datamex
Top achievements
Rank 2
answered on 19 May 2011, 08:14 AM
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
0
Shinu
Top achievements
Rank 2
answered on 19 May 2011, 11:47 AM
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.
Tags
ListBox
Asked by
Datamex
Top achievements
Rank 2
Answers by
Datamex
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Share this question
or