user3492682
user3492682

Reputation: 17

To check a field type is textbox or not

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

Answers (3)

Jeppe Stig Nielsen
Jeppe Stig Nielsen

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

huMpty duMpty
huMpty duMpty

Reputation: 14470

Use Object.GetType

if(FieldTypeInfo.GetType()== typeOf(TextBox))
{
}

Or is

if (FieldTypeInfo is DropDownList)
{
}

Upvotes: 1

Carsten
Carsten

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

Related Questions