c# - RadioButtonList null values -


have radiobutton list filling strings , know how in given time value of selected element , throw string example. command selectedvalue , selecteditem have null values​​.

this radio button list filled several times during execution of same page.

//where fill radiobuttonlist public void mostraimagenscarrefadas() {     list<string> files = new manfile().getlistfilesfordirectory(this, mae.diretorio_temp_img);      rbimagemprincipal.items.clear();      if (files != null)     {         foreach (string item in files)         {             rbimagemprincipal.items.add(new listitem(item));         }     } }   //as in aspx <div> <asp:radiobuttonlist id="rbimagemprincipal" runat="server" repeatdirection="vertical" autopostback="false" onselectedindexchanged="rbimagemprincipal_selectedindexchanged"></asp:radiobuttonlist> 

 //where encounter null values ​​when trying selected item (clicked)  //nothing value obtained direferente null. if (rbimagemprincipal.selecteditem != null)                 {                     if (rbimagemprincipal.selecteditem.tostring() == str)                     {                         imagem.imagemprincipal = "sim";                      }                 } 

it seems you're populating radiobuttonlist on page load - if - make sure surround population of radiobuttonlist if/then/postback block: if not page.ispostback ' populate rbl end if

eg:

        if (!ispostback)         {             loadradiobuttonlist();         }