Search code examples
asp.netiterationdatalistitem

How to find the DataListItem item type without using FindControl?


I want to be able to determine if my DataListItem type is of type RadioButton in C# code behind.

Is this possible?

Alternatively if it is not type DropDownList that would also work.

Isn't there a way to some kind of check such as

if(item.ItemType.Equals(HtmlInputRadioButton)){ // }


Solution

  • item.ItemType is an enum. Type will never be HtmlInputRadioButton

      public enum ListItemType
      {
        Header,
        Footer,
        Item,
        AlternatingItem,
        SelectedItem,
        EditItem,
        Separator,
        Pager,
      }
    

    Instead, the code should be like this -

    void Item_XXXX(Object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item ||
            e.Item.ItemType == ListItemType.AlternatingItem)
        {
            // Make sure MyRadioButtonId is an ID of HtmlInputRadioButton
            var htmlInputRadioButton = e.Item.FindControl("MyRadioButtonId") 
              as HtmlInputRadioButton;
        }
    }