Link to home
Start Free TrialLog in
Avatar of Camillia
CamilliaFlag for United States of America

asked on

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...
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

if (ddl_1.SelectedValue == ddl_2.SelectedValue && ddl_2.SelectedValue == ddl_3.SelectedValue && ddl_2.SelectedValue == ddl_4.SelectedValue)
{
    // same
}
else
{
    // different
}
Avatar of Camillia

ASKER

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??
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??
ASKER CERTIFIED SOLUTION
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
let me try it now. I'm at work now. Thanks for sticking with this . Kamila.
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

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);
}
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.
You have: for (int i = 0l i < 1; i++)

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

?
something likr that running the loop for first item
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.
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??
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
yeah, you're right.
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.