-2

I need to validate 2 CheckBoxLists together. Meaning i can check one box from either list and the validation has to pass. What would be the proper way of implementing something like this ?

I'm tempted to extend CustomValidator, add a property ControlValueId2 to pass the ID of my second CheckBoxList, but i can't help but feel there must be a better way.

The reason i use 2 checkboxlists is because the data is coming from 2 different database with different schemas and even if they look the same, under the hood they are different. I don't think you can bind 2 datasources to one checkboxlist ?

Kinda new to webforms so any insight would be greatly appreciated. Sry i cannot post any code, but i hope i was clear enough!

Narcil
  • 325
  • 2
  • 13
  • 1
    Could you explain further what you mean by *validating* and what the one CheckBoxList has to do with the other? – Leon Bohmann Aug 07 '15 at 14:42
  • 1
    by validating i mean at least one checkbox has to be checked from either list. both look the same to the user and are functionnaly similar but the data comes from 2 different databases. so i aggregate 2 lists from 2 different database into 1 choice for the user. – Narcil Aug 07 '15 at 14:46
  • I'm not sure it's entirely clear but I have a suspicion that this is what you're after: http://stackoverflow.com/questions/1228112/how-do-i-make-a-checkbox-required-on-an-asp-net-form – Adam Drew Aug 07 '15 at 14:50
  • it isn't but thx. which part isn't clear? – Narcil Aug 07 '15 at 15:20
  • Sorry, missed your last comment so didn't have the full picture. Could you not aggregate both of your datasources in the code behind, then assign it to one checkbox list. Something like this: `foreach(var item in dataSource1) checkboxlist1.Items.Add(new(ListItem(item.DisplayField, item.ValueField)` And then do the same for the second checkbox list? If that's correct I'll put it into a full answer – Adam Drew Aug 07 '15 at 15:54

1 Answers1

0

Validating two controls at the same time would be a bit of a pain. I'd tend toward the easier solution of aggregating both your datasources together, into one checkbox list.

public void Page_Load(Object sender, EventArgs e)
{
   // Get items from database table 1
    var items1 = GetTable1Data();
    foreach(var item in items1)
    {
       this.checkboxList1.Items.Add( new ListItem(item.DisplayMember, item.ValueMember));
    }

    // Get items from database table 2
    var items2 = GetTable2Data();
    foreach(var item in items2)
    {
       this.checkboxList1.Items.Add( new ListItem(item.DisplayMember, item.ValueMember));
    }
}

Then you should just be able to use a requiredfieldvalidator on the single checkboxlist

Adam Drew
  • 155
  • 8
  • thx i'll probly refactor my code then. i hesitated allot between the 2 and of course i picked the wrong one :( – Narcil Aug 08 '15 at 11:42