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 whenIncluir
is called?– Leonel Sanches da Silva
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.
– Tobias Mesquita
Using Messagebox.Show on web pages is exactly what shouldn’t happen. Try using Selectedvalue from Dropdownlist.
– pnet
The problem is that _Processes is empty, you can save the _Processes in a Session or reload each Post.
– PauloHDSousa