WEI ZHUANG GOH
WEI ZHUANG GOH

Reputation: 333

How to check a dropdownlist exist or not in C# aspnet

How can I check a dropdownlist is existed on a page or not? I want to validate a certain condition when there is no dropdownlist found on the page. If it cant find any dropdownlist, it will not run the code string test = DropDownList1.SelectedItem.Text.ToString();

What i've tried:

if (DropDownList1 not exist) 
{ 
    //
}
string test = DropDownList1.SelectedItem.Text.ToString();

Upvotes: 0

Views: 57

Answers (1)

Fuzzy
Fuzzy

Reputation: 496

if (DropDownList1 != null) 
{ 
    string test = DropDownList1.SelectedItem.Text.ToString();
}

Or alternatively:

if (DropDownList1 == null) 
{ 
   //Do stuff
   return;
}
string test = DropDownList1.SelectedItem.Text.ToString();

Upvotes: 1

Related Questions