[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

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...
0
Camillia
Asked:
Camillia
  • 10
  • 5
1 Solution
 
Anurag ThakurCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Anurag ThakurCommented:
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
 
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 ThakurCommented:
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 ThakurCommented:
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 ThakurCommented:
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

  • 10
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now