简体   繁体   English

如何访问ASP.NET中的RadioButtonList项目?

[英]How to access RadioButtonList items in ASP.NET?

Let's say I have a RadioButtonList in C#. 假设我在C#中有一个RadioButtonList。 This list contains a selector for favorite fruits (sorted alphabetically): 此列表包含最喜欢的水果的选择器(按字母顺序排列):

Favorite Fruits:
    ( )  Apple
    ( )  Banana
    ( )  Pineapple
    ( )  Pomegranate

...and let's say I have a method that prints out facts about your favorite fruit at the click of a 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 say there are dozens of switch/cases in my codebase that depend on which favoriteFruits element is selected. 假设我的代码库中有数十个开关/案例,这取决于选择了哪个favoriteFruits元素。

If I decide to add an element to this list (not at the end), I'd have to manually find every switch/case and update the indices (adding Banana would force me to +1 the indices of Kiwi , Pineapple , and Pomegranate , if I wanna keep everything alphabetical.) 如果我决定将元素添加到此列表中(而不是在结尾处),则必须手动查找每个开关/案例并更新索引(添加Banana会迫使我对KiwiPineapplePomegranate的索引+1 ,如果我想按字母顺序排列所有内容。)

Is there a way I can reference the indices as an enumerated value? 有没有一种方法可以将索引作为枚举值引用? As in, is there something that I can do that's similar to this? 在这种情况下,我能做些什么吗?

    switch( favoriteFruits.SelectedIndex ){
        case favoriteFruits.getEnumConstants.APPLE:
            doStuffForApple();
        break;

I know there's a RadioButtonList.Items accessor, but I'm stumped as to where to go from there. 我知道有一个RadioButtonList.Items访问器,但是我很困惑从那里去哪里。 Any help is appreciated! 任何帮助表示赞赏!

Is there a way I can reference the indices as an enumerated value? 有没有一种方法可以将索引作为枚举值引用?

Based on your entire post it seems that, "you need to know what is clicked and take action(s) based on that". 根据您的整个帖子,看来,“您需要知道单击的内容并根据该内容采取措施”。 It really doesn't matter then if you will be testing against the index or the value of the control. 然后,是否要针对索引或控件的值进行测试并不重要。 If that's the case then doing the following should help: 如果是这样,那么执行以下操作应该会有所帮助:

// declare the items, no need to set values
public enum FavoriteFruits {
    Banana,
    Apple, }

// bind the enum to the control
RadioButtonList1.DataSource = Enum.GetValues(typeof(FavoriteFruits)); 
RadioButtonList1.DataBind();

// this is needed because SelectedValue gives you a string...
var selectedValue = (FavoriteFruits)Enum.Parse(typeof(FavoriteFruits), 
   RadioButtonList1.SelectedValue, true); 
//...then you can do a switch against your enum
switch (selectedValue )
{
    case FavoriteFruits.Apple:
        doStuffForApple();
        break;
    case FavoriteFruits.Banana:
       doStuffForBanana();
       break;
}

Make sure to validate the SelectedValue as it will throw an exception if nothing is selected. 确保验证SelectedValue因为如果未选择任何内容,它将引发异常。

If I understood correctly, you would use enums? 如果我理解正确,您会使用枚举吗?

enum Fruits{
    Apple = 0,
    Banana = 1,
    Pineapple = 2,
    Pomegranate = 3
};

switch( favoriteFruits.SelectedIndex ){
    case Fruits.Apple:
        doStuffForApple();
    break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM