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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Anurag ThakurTechnical 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.