Get items from a List<String> - ASP.NET

Asked

Viewed 1,012 times

2

I am beginner in ASP.NET development with C# and, I have the following question:

When loading a page, I run a method that fills a Dropdownlist. This Dropdownlist is part of a form and, after it is completed and the user clicks the "Include" button, the Include method will be triggered().

My intention is to get the Index of the selected DDL option because, based on this number, I get the ID that was stored while loading the page in a List

Look at the code:

    List<String> _Processos = new List<String>();
    List<String> _Unidades = new List<String>();


    protected bool Incluir() 
    {
        bool r = false;

        System.Windows.Forms.MessageBox.Show(cbUnidadeIncidente.SelectedIndex.ToString());

        OC o = new OC();
        o.IdUsuario = (String)Session["IDUsuario"];
        o.Dt = DateTime.Parse(txtDtOcorrencia.Text);
        o.Descricao = Kompaktor.K.KSTR(txtDescricao.Text);
        o.UnidadeIncidente = _Unidades[cbUnidadeIncidente.SelectedIndex];
        o.UnidadeAfetada = _Unidades[cbUnidadeAfetada.SelectedIndex];
        o.Observacao = txtObservacao.Text;
        o.Processo = _Processos[cbProcesso.SelectedIndex];
        o.Perda = Kompaktor.K.KSTR(txtPerda.Text);
        o.Solucao = Kompaktor.K.KSTR(txtSolucao.Text);
        o.Incluir();

        return r;
    }

However, during the execution, just the first line that tries to do the procedure of trying to catch the Index triggers an error, saying that the value of the array of the list has been exceeded.

I already know that the problem is in List<>, after all, using a messagebox, the value of the sealing is shown. It seems that the lists are reset when loading. And now, how can I proceed (so that these lists are visible throughout the class) ?

From now on, thank you!

  • In his example _Processos is empty. That’s the way it is when Incluir is called?

  • Due to the Lifecycle of Webforms, it ends up creating a new object with each request, so its list is not available during Postback, in this case you can put the lists in a Session, Viewstate, Cache. But depending on the case it is preferable to fill out the list again.

  • Using Messagebox.Show on web pages is exactly what shouldn’t happen. Try using Selectedvalue from Dropdownlist.

  • The problem is that _Processes is empty, you can save the _Processes in a Session or reload each Post.

1 answer

1

I was in doubt of yours Dropdownlist and how you describe it in your code. "cbProcesso".. (cb) for me is initials of Checkbox ... but.... look at the way you’re carrying the Dropdownlist and also how you keep the data on your list.

See how to keep a Session of your list.

To carry

Session["_Processos"]) = _Processos;

to recover

List<String> _Processos = Session["_Processos"]) ;

Another way would be for you to upload your list would be like this. Exe:

private List<String> m_Processos = null;
        private List<String> Processos
        {
            get
            {
                if (m_Processos == null)
                {
                    try
                    {
                         List<String> _Processos = new List<String>(){"teste1","teste2","teste3"};

                         m_Processos = _Processos;
                    }
                    catch (Exception Exc)
                    {
                        //"lErro";
                    }
                }
                return m_Processos;
            }
        }

and then just call

o.Processo = Processos[cbProcesso.SelectedIndex];

is good for a breakpoint in your code to see what is going through.. inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.