How to get a Textbox from an Accordion that has been dynamically generated?

Asked

Viewed 105 times

1

It follows the code of how I create the Accordion

   private void CarregaAccordionCarros()
   {                        
                //DataTable da Consulta
                var dtTable = GetDataCarro();


                AjaxControlToolkit.AccordionPane AccordionPane = null;
                Table tabela = null;

                foreach (DataRow dtRow in dtTable.Rows)
                {                    
                    AccordionPane = new AjaxControlToolkit.AccordionPane();
                    Label label;                    

                    label = new Label();
                    label.Text = dtRow[1].ToString();

                    //Head
                    AccordionPane.HeaderContainer.Controls.Add(label);

                    var textBox = new TextBox();
                    textBox.ID = "TextBoxCarro";
                    textBox.Text = dtRow[2].ToString();

                    var td = new TableCell();                    
                    td.Controls.Add(textBox);

                    var tr = new TableRow();
                    tr.Controls.Add(td);

                    tabela = new Table();   
                    tabela.Controls.Add(tr);

                    //Content
                    AccordionPane.ContentContainer.Controls.Add(tabela);

                    //Accordion
                    AccordionPergJustificativa.Panes.Add(AccordionPane);

                }
            }

Here is the Event in which I intend to take Textbox values.

Accordioncarros.Panes always come == 0

protected void ButtonSalvarCarro_Click(object sender, EventArgs e)
{
    try 
        {           
            foreach (AccordionPane panes in AccordionCarros.Panes)
            {
                var textBox = (TextBox)panes.FindControl("TextBoxCarro");
                var str1 = textBox.Text;
            }
        }
        catch (Exception)
        {

            throw;
        }            
}

I left the Try Catch in the code, because here no exception, but checking by the immediate concigo see that the Accordioncarros.Panes always comes zeroed.

  • Why did you use this try-catch that does nothing?

  • Thanks for the comment. Missed explain why Try Catch. Now I’ve edited the question.

  • @alexanderPataki you have how to post the Page_Load of your page, because depending on how you load your acordion by triggering the button you may be losing the information of viewstate

  • Another thing I’ve identified, I can’t find where in your method CarregaAccordionCarros is created the TextBoxCarro, you will have to name your components at the time you add them to Accordion label.ID = "NomeDoLabel";

  • @Pablovargas I edited the code again to show where I put the textbox id. In Page_load, you have nothing because I press Accordion from a button event.

  • So try your own ButtonSalvarCarro_Click call again the CarregaAccordionCarros, this before the foreach

  • @Pablovargas It was a good idea, since he would create everything again. But it also did not work.

  • @alexanderPataki, you use some UpdatePanel? Put the code aspx if possible.

Show 3 more comments
No answers

Browser other questions tagged

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