简体   繁体   English

访问动态RadioButtonList数组对象

[英]Accessing a dynamic RadioButtonList array object

I've created an array of RadioButtonList class, but apparently can't seem to access it or use the answer retrieved from it. 我已经创建了RadioButtonList类的数组,但显然似乎无法访问它或使用从中检索到的答案。 I always get the exception: Object reference not set to an instance of an object 我总是得到异常:对象引用未设置为对象的实例

  static int jimmy = 0;


    protected void Button5_Click(object sender, EventArgs e)
    {
        int sizeOfPain = GlobalVariables.sympLCWR1Pain.Count;

        RadioButtonList[] RBLPain = new RadioButtonList[sizeOfPain];

        Label1.Visible = false;
        RadioButtonList1.Visible = false;


        Label[] Labella = new Label[sizeOfPain];

        if (jimmy < sizeOfPain)
        {
            Labella[jimmy] = new Label();
            RBLPain[jimmy] = new RadioButtonList();

            Labella[jimmy].Text = GlobalVariables.sympLCWR1Pain[jimmy];

            RBLPain[jimmy].Items.Add("Yes");

            RBLPain[jimmy].Items.Add("No");

            Panel1.Controls.Add(Labella[jimmy]);
            Panel1.Controls.Add(RBLPain[jimmy]);



                if (RBLPain[jimmy].SelectedIndex == 0)
                {
                    GlobalVariables.sympLCWR1Yes.Add(GlobalVariables.sympLCWR1Pain[jimmy]);
                }


        }

        else
        {
            Label2.Text = "YOUS DONE!";

                Label3.Text = GlobalVariables.sympLCWR1Yes[0];

            Button5.Visible = false;

        }  

        jimmy++;


    }

i get the exception at the if condition. 我在if条件下得到了例外。 Any help would be appreciated thanks :) 任何帮助,将不胜感激,谢谢:)

What that error means is that you are trying to access something that hasn't yet been instantiated. 该错误意味着您正在尝试访问尚未实例化的内容。 In your updated code, I see you have the following within your click event handler: 在更新的代码中,我看到您的click事件处理程序中包含以下内容:

 RadioButtonList[] RBLPain = new RadioButtonList[sizeOfPain];
 Label[] Labella = new Label[sizeOfPain];

This means that every time the click event is handled, you are redeclaring the RBLPain and Labella arrays. 这意味着每次处理click事件时,您都在重新声明RBLPainLabella数组。 Also, when execution leaves leaves the handler, the variables fall out of scope, so you will not be able to use them in other functions, or use the changes made within the handler from one call to the next. 同样,当执行离开处理程序时,变量将超出范围,因此您将无法在其他函数中使用它们,也无法使用在处理程序中从一个调用到下一个调用的更改。 I don't know what the rest of your code is doing, but despite the seemingly unnecessary arrays, execution should survive your click event. 我不知道您的其余代码在做什么,但是尽管看似不必要的数组,但执行应该在您的click事件中保留下来。

In your original post you were trying to access the SelectedItem.Text property of the RBLPain[jimmy] . 在您的原始帖子中,您试图访问RBLPain[jimmy]SelectedItem.Text属性。 In this revision you are checking the SelectedIndex instead. 在此修订版中,您正在检查SelectedIndex When SelectedIndex is -1, SelectedItem will be null, perhaps this led to your original problem. SelectedIndex为-1时, SelectedItem将为null,这可能导致您原来的问题。 Regardless of what is changed on your form, because you are creating a new RadioButtonList during every click event, you are not working with the values from your form - SelectedIndex will always be -1 from what I can see. 不管表单上有什么更改,因为您在每次click事件期间都创建了一个新的RadioButtonList ,所以您无法使用表单中的值-从我所看到的内容中, SelectedIndex始终为-1。

I dont understand why you checking the condition .If you are creating rbl on buttonclick first item should always get selected. 我不明白为什么要检查条件。如果在buttonclick上创建rbl,则始终应选择第一项。 Anyway use RBLPain[jimmy].SelectedIndex=0; 无论如何都使用RBLPain[jimmy].SelectedIndex=0; before if condition. 如果条件之前。

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

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