locked
Convert object to type RRS feed

  • Question

  • User-284642143 posted

    I have two controls, a radiobuttonList and CheckboxList. I would like to retrieve the items selected within these controls.

     	private void ItemsInList(object c)
            {
                if (c == typeof(RadioButtonList))
                {
     
                }
                else if (c == typeof(CheckBoxList))
                {
    
                }
    
                foreach (ListItem li in c.Items)
                {
    
                }
            }

    What i'm trying to do is pass in a control (c) and then determine if its a RBL or CBL (if i need to that is) and then iterate through the list and take some action.

    At present i have the above code but receive the error

    'object' does not contain a definition for 'Items' and no extension method 'Items' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?

    Im not entirely sure if this is possible or if im doing something wrong?

    Monday, November 18, 2019 11:39 AM

Answers

  • User753101303 posted

    Hi,

    According to the doc both RadioButtonList and CheckBoxList are inheriting from the ListControl class which provides the Items property so try :

    private void ItemsInList(ListControl c)

    Edit: in short you should work against the more general type that provides what your code actually use

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 18, 2019 11:50 AM

All replies

  • User753101303 posted

    Hi,

    According to the doc both RadioButtonList and CheckBoxList are inheriting from the ListControl class which provides the Items property so try :

    private void ItemsInList(ListControl c)

    Edit: in short you should work against the more general type that provides what your code actually use

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 18, 2019 11:50 AM
  • User-284642143 posted

    Thanks, been reading on Generics recently and over thought this too much.

    Monday, November 18, 2019 12:05 PM