How can I check all dropdownlist values are the same?

I have 4 dropdownlists. User selects from each dropdownlist. I want compare the ddl SelectedValues and see if they're all the same:

ddl -- select A item
ddl 2 --select A item
ddl 3 -- Select A item
ddl3 -- select A item

So, i want to compare the SelectedValue and see if the values are ALL equal..for example to A...or user has selected different selectedValues...
LVL 8
CamilliaAsked:
Who is Participating?
 
Anurag ThakurConnect With a Mentor Technical ManagerCommented:
using find control get the reference to the first drop down list
declare a boolean variable like areDDLSame = true
create a loop in which  you get the reference to the second, third etc etc ddls and compare the value of the first ddl with the others. till the time your values match you take no action and if the value is different set the boolean value to false and break from the for loop
depending on the value of the boolean flag you take necessary action
0
 
Anurag ThakurTechnical ManagerCommented:
if (ddl_1.SelectedValue == ddl_2.SelectedValue && ddl_2.SelectedValue == ddl_3.SelectedValue && ddl_2.SelectedValue == ddl_4.SelectedValue)
{
    // same
}
else
{
    // different
}
0
 
CamilliaAuthor Commented:
ok, in my example..i have 4 ddls . I actually should've said the number the ddls is dynamic. it can be one or 2 or 100...

how can do that??
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
CamilliaAuthor Commented:
this ddl is in a grid. when grid binds...grid can have 1 row or 10 rows or more. so maybe...

loop thru the grid rows to see how many ddls i have, then compare??
0
 
CamilliaAuthor Commented:
let me try it now. I'm at work now. Thanks for sticking with this . Kamila.
0
 
CamilliaAuthor Commented:
OK, i dont know how to get the FIRST ddl value. I get null. The loop below works BUT it gets me all the DDLs..not just the first one. Not sure how to change it to get the first value.

This is Telerik grid..it's like AJAX:



 
 DropDownList ddlSelection = (grdOrderItems.MasterTableView.FindControl("ddlNextStatus") as DropDownList);
 string test = ddlSelection.SelectedValue; //*** THIS GIVES ME NULL
 
foreach (GridDataItem dataItem in grdOrderItems.MasterTableView.Items)
        {
            DropDownList ddlSelectionOthers = (dataItem.FindControl("ddlNextStatus") as DropDownList); //** this works because of I'm in foreach loop ..there has to be a way..
     if (ddlSelection.SelectedValue == ddlSelectionOthers.SelectedValue)
        ....

Open in new window

0
 
Anurag ThakurTechnical ManagerCommented:
instead of that way try this and ignore the first ddl value in the for each loop
DropDownList ddlSelectionOthers;
for (int i = 0l i < 1; i++)
{
      ddlSelectionOthers = (grdOrderItems.MasterTableView.Items[i].FindControl("ddlNextStatus") as DropDownList);
}
0
 
CamilliaAuthor Commented:
let me try it. Been at this all morning and I dont want to ask my coworker..he loves himself too much :) thanks again..Kamila.
0
 
CamilliaAuthor Commented:
You have: for (int i = 0l i < 1; i++)

Is that for (int i=0; i<1; i++)

?
0
 
Anurag ThakurTechnical ManagerCommented:
something likr that running the loop for first item
0
 
CamilliaAuthor Commented:
yes, that gives me the first value of the ddl. It's beautiful:)

>>nore the first ddl value in the for each loop
let me work on that. Thanks,. Will post back.
0
 
CamilliaAuthor Commented:
You know what, that's my problem again..now I have the first item..how do I skip the first item in the foreach loop??
0
 
Anurag ThakurTechnical ManagerCommented:
i think do nothng for that
if we ignore that then it will be a slight overhead but as both are same the comparison will be true and the processing will not fail so no harm done
i hope it helps - ragi
0
 
CamilliaAuthor Commented:
yeah, you're right.
0
 
CamilliaAuthor Commented:
i have another problem. I will close this question because your answer solved my issue. Then came across another issue that i hadnt thought of. Thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.