Reputation: 17
In the below code how to check whether the field is textbox,dropdownlist,checkbox in asp.net.
if (FieldTypeInfo == TextBox)
{
}
if (FieldTypeInfo == DropDownList)
{
}
public FieldType FieldTypeInfo { get; set; }
public enum FieldType
{
TextBox,
DropDownList,
SearchList,
CheckBox,
Date
}
Upvotes: 0
Views: 530
Reputation: 62002
After your edit where we see you have an enum, the solution is:
if (FieldTypeInfo == FieldType.TextBox)
{
...
}
if (FieldTypeInfo == FieldType.DropDownList)
{
...
}
However, strongly consider using a switch
statement, for example:
switch (FieldTypeInfo)
{
case FieldType.TextBox:
...
break;
case FieldType.DropDownList:
...
break;
default:
...
break;
}
Upvotes: 0
Reputation: 14470
Use Object.GetType
if(FieldTypeInfo.GetType()== typeOf(TextBox))
{
}
Or is
if (FieldTypeInfo is DropDownList)
{
}
Upvotes: 1
Reputation: 11636
You can use the is
keyword in order to check types:
if (FieldTypeInfo is TextBox)
{
var text = ((TextBox)FieldTypeInfo).Text;
// ...
}
else if (FieldTypeInfo is DropDownList)
{
// ...
}
Upvotes: 2