Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi friends,

am facing problem with dropdownlist Selection

I want to Select a Item from dropdwonlist, but i can select this value also
----- Select -----

everything is fine here, but user shouldnot select this from dropdownlist ---Select ----

so i want to avoid selection this value from dropdownlist ,

please can you help me,

Thanks
Posted
Comments
Prasad Khandekar 18-Jun-13 6:27am    
How do you distinguish between yorself and other user? My simple guess is via session? If it's correct then add a javascript validation or server side validation to flag an error if user is other than you and has selected ---- Select ---- Option.

Regards,
Suman Zalodiya 18-Jun-13 6:55am    
Ranjith please can you clarify your question. Do you want that you can select the option ---Select--- and user should not able to select. Or you want that ---Select--- should display but no one should select it and you want it to show ---Select--- for information purpose only.

1 solution

Hi Ranjith,

Please try this.!

C#
// If you want a list of options to be disabled 
foreach ( ListItem item in dropdownlist.Items )
{
    if ( //condition if item's value == 0)
    {
        item.Attributes.Add( "disabled", "disabled" );
        // or
        // item.Enabled = false;
    }
}


or

C#
// Specifically for the 1st option. Throws error when you dont have any options in the dropdownlist. So, becareful with this.
dropdownlist.Items[0].Attributes.Add("disabled", "disabled");


If not the above, please do try the below one.

C#
item.Enabled = false;


Personally, I've not checked it but should work fine. Let me know if you have any issues. I've provided "disable using attributes" and "Enable" options. Please test it for yourself with both options and use the best one that suits your application.

Thank you,
RelicV
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900