简体   繁体   中英

get a value from a radio button

I have a radio button in my .aspx page and in the code behind I have written the following:

if (rbOnlyCreatedorUpdated.Checked == true)
{
    searchObject.CreatedOrUpdatedOnValidDate = SearchCritera.OnlyCreatedOrUpdated;
}
else if (rbOnlyOld.checked == true)
{
    searchObject.CreatedOrUpdatedOnValidDate = SearchCritera.OnlyOld;
}
else
{
    searchObject.CreatedOrUpdatedOnValidDate = SearchCritera.AllChanged;
}

And I really dislike the above. It feels clumsy and unclean. I would like a value to be returned by the radio button itself (named rbOnlyCreatedOrUpdatedOnValidDate, ie the GroupName).

Is it possible or is the above the correct way to get the value?

If you use a RadioButtonList control, you can access the selected radiobuttons value by using IdOfRadioButtonList.SelectedValue . This will give you a string, so you will still have to do the conversion to one of the SearchCriteria class' properties yourself...

If it makes you feel any better, you can make it into a ternary operator, like so:

searchObject.CreatedOrUpdatedOnValidDate = 
    rbOnlyCreatedorUpdated.Checked ? SearchCritera.OnlyCreatedOrUpdated : 
    rbOnlyOld.checked ? SearchCritera.OnlyOld : 
    SearchCritera.AllChanged;

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