Latest

random

How to check DropDownList control contains a value or not? - ASP.Net

C# - Different way to check if a drop down list contains a value

In this blog post I would like to share about the scenario like If you have to check a drop down list contains a value or not in asp.net web application. There are different ways to implement If drop down list contains a value? 

Some people are looping through all items and finding the item exist or not. But in the following C# sample you will find the simple and different ways to check if the item exist in the drop down or finding a value in DropDownList control in Asp.Net. Find the code below:-
check if a drop down list contains a value, check, if the item exist in the drop down, Dropdown List - Check if value exists, drop down list from a value
Check if a drop down list contains a value
Default.aspx
<asp:DropDownList ID="ddlTest" runat="server">
        <asp:ListItem Text="Select"></asp:ListItem>
        <asp:ListItem Value="ABC"></asp:ListItem>
        <asp:ListItem Value ="XYZ"></asp:ListItem>
</asp:DropDownList
simple code sample , c#, asp.net
Dropdown List - Check if value exists
Default.aspx.cs
if (ddlTest.Items.FindByValue("XYZ") != null)
    {
                ddlTest.SelectedValue = "xyz";
    }


OR

if (ddlMobileNumber.Items.FindByText(
    GetMobileNumberCookie().ToString()) != null)
    {
      // .. your code here
    }
OR
if (ddlMobileNumber.Items.Contains(new 
    ListItem(GetMobileNumberCookie().ToString())))
     {
         // .. your code here
     }


I hope you will enjoy the development tip while programming with C# in asp.net web application. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome. 

Also If you like this article, don't forget to share this article with your friends and colleagues.

No comments:

All Rights Reserved by Etechpulse © 2015 - 2016
Powered By Blogger

Contact Form

Name

Email *

Message *

Powered by Blogger.