0

I want to make a survey where you can select 4 people (from a lookup list I have made).

My idea is to make 1 drop down list for each selection but how do I make sure that the survey cannot be submitted unless there is a selection in all four dropdowns AND that these are unique to the response (that is you may not select the same person twice)?

2 Answers 2

2

If it was me I would on "selectedIndexChanged" event check to see if the selection is unique if it is then persist changes otherwise tell the user it needs to be or better yet when a user selects one then it gets removed from the list so you dont have the option to select the same value again! all the DropDownList or what ever your going to use, you can all use the same event method like this:

DropDownList ddl = new DropDownList();
ddl.SelectedIndexChanged +=new System.EventHandler(ddl_SelectedIndexChanged);

That is if you are creating it through c# code in createChildControls and then for each one just keep on adding:

ddl.SelectedIndexChanged +=new System.EventHandler(ddl_SelectedIndexChanged);

You have to make sure each one has its own ID or you will get an error: "there are multiple controls with the same ID "ddl", each control requiers a unique ID" well its somthing on the lines of that ;) .

so it would look somthing like so:

protected override void CreateChildControls()
{
   int intValue = 0;
   DropDownList ddl = new DropDownList();
   ddl.ID = "ddl"+ intValue;
   ddl.SelectedIndexChanged +=new System.EventHandler(ddl_SelectedIndexChanged);
   intValue++;
   ddl.ID = "ddl"+ intValue;
   ddl.SelectedIndexChanged +=new System.EventHandler(ddl_SelectedIndexChanged);
   intValue++;
   ddl.ID = "ddl"+ intValue;
   ddl.SelectedIndexChanged +=new System.EventHandler(ddl_SelectedIndexChanged);
   this.Controls.Add(ddl);
}

private void ddl_SelectedIndexChanged(object sender, System.EventArgs e)
{
    //do your check
}

If you wack it in a for loop and then itwould be easir to make ;) without repetative code.

To do it in ASP.net should be relativly easy!

On the submit button you can perform a check to see all values have been selected and none are the same. If they are then move on else show message to user.

0

I would add each answer to a collection such as a List or a Dictionary/Tuple if I needed to pair some other information with it.

Then I would check to make sure this value did not already exist in my collection when the survey was submitted.

e.g.

foreach (var item in myCollection)
{
if (item != newAnswer)
    myCollection.Add(item);
else
    throw Exception("Answer is not unique!");
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.