c# - How to access RadioButtonList items in ASP.NET? -


let's have radiobuttonlist in c#. list contains selector favorite fruits (sorted alphabetically):

favorite fruits:     ( )  apple     ( )  banana     ( )  pineapple     ( )  pomegranate 

...and let's have method prints out facts favorite fruit @ click of button:

private void fruitfactsbutton_onclick(arguments){      switch( favoritefruits.selectedindex ){          case 0:      // apple           dostuffforapple();         break;          case 1:      // banana           dostuffforbanana();         break;          case 2:     // pineapple           dostuffforpineapple();         break;          case 3:    // pomegranate           dostuffforpomegranate();         break;     }  } 

let's there dozens of switch/cases in codebase depend on favoritefruits element selected.

if decide add element list (not @ end), i'd have manually find every switch/case , update indices (adding banana force me +1 indices of kiwi, pineapple, , pomegranate, if wanna keep alphabetical.)

is there way can reference indices enumerated value? in, there can that's similar this?

    switch( favoritefruits.selectedindex ){         case favoritefruits.getenumconstants.apple:             dostuffforapple();         break; 

i know there's radiobuttonlist.items accessor, i'm stumped go there. appreciated!

is there way can reference indices enumerated value?

based on entire post seems that, "you need know clicked , take action(s) based on that". doesn't matter if testing against index or value of control. if that's case doing following should help:

// declare items, no need set values public enum favoritefruits {     banana,     apple, }  // bind enum control radiobuttonlist1.datasource = enum.getvalues(typeof(favoritefruits));  radiobuttonlist1.databind();  // needed because selectedvalue gives string... var selectedvalue = (favoritefruits)enum.parse(typeof(favoritefruits),     radiobuttonlist1.selectedvalue, true);  //...then can switch against enum switch (selectedvalue ) {     case favoritefruits.apple:         dostuffforapple();         break;     case favoritefruits.banana:        dostuffforbanana();        break; } 

make sure validate selectedvalue throw exception if nothing selected.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -