简体   繁体   中英

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
    }

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)
{
    // ...
} 

Use Object.GetType

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

Or is

if (FieldTypeInfo is DropDownList)
{
}

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM