locked
how to get dynamically generated textboxes values and store them in array? RRS feed

  • Question

  • User-1874889200 posted

    Hello All,

    This is my first ever post in this forum, i have searched everywhere to find a solution, but none worked out for me, so now iam posting here to get your help.

    I have to store categories along with their subcategories. the number of subcategories can vary depending on which category that's why iam using dynamic textboxes to get the subcategories. for example, Category A has 3 sub categories so i have a dropdownlist which stores numbers from 1 to 10, and then the user selects 3 and three textboxes are shown. this works perfectly. but when i want to click the button to save these three textbox values, then it fails.

    my code to generate the textboxes:

     for (int i = 0; i <= numControls-1; i++)
                {
                    lbl = new Label();
                    txt = new TextBox();
                    lbl.Text = "بخش فرعی " + (i).ToString() + " ";

                    // txt.Text = "TextBox" + (i).ToString();

                    lbl.ID = "Label" + i.ToString();

                    txt.ID = "TextBox" + i.ToString();

                    pnlDynamic.Controls.Add(lbl);
                    pnlDynamic.Controls.Add(txt);

                     //_textBoxes.Add(txt);
                    
                    pnlDynamic.Controls.Add(new LiteralControl("<br /><br />"));
                }

    and the code to get the values with the click event of button save:

     protected void addCategoryBtn_Click(object sender, EventArgs e)
        {
          
       for (int i = 0; i <= numControls - 1; i++)
            {
                lblmsg.Text = txt.Text;
            }

        }

    I also tried this code but still no result:

     //if (IsPostBack)
            //{
            //    foreach (TextBox t in ((List<TextBox>)Session["AddedControls"]))
            //    {
            //        //TextBox txtCtrl = (TextBox)pnlDynamic.FindControl("TextBox" + i.ToString());

            //        //if (txtCtrl != null)
            //        //{
            //        //    lblmsg.Text = lblmsg.Text + ", " + txtCtrl.Text;
            //        //}
            //        lblmsg.Text = t.Text;
            //        //lblmsg.Text = txt.Text;
            //    }
            //}

    Can anyone please help me with this, it's been a week iam working and searching.. Thanks

    Sunday, October 21, 2012 5:54 AM

Answers

  • User-735357247 posted

    Actually speaking it has to retrieve the text of the textboxes as well. I have tried the similar code and it is absolutely working fine.

    Do one thing. You just create a new solution and paste the below code and let me know whether it is working or not?

    protected void Page_Load(object sender, EventArgs e)
        {
            Label lbl;
            TextBox txt;
            for (int i = 0; i <= 3; i++)
            {
                lbl = new Label();
                txt = new TextBox();
                lbl.Text = "Label" + (i).ToString() + " ";
    
                // txt.Text = "TextBox" + (i).ToString();
    
                lbl.ID = "Label" + i.ToString();
    
                txt.ID = "TextBox" + i.ToString();
                txt.EnableViewState = true;
    
                Panel1.Controls.Add(lbl);
                Panel1.Controls.Add(txt);
    
                //_textBoxes.Add(txt);
    
                Panel1.Controls.Add(new LiteralControl("<br /><br />"));
            }
            //divText.InnerText
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (Control c in Panel1.Controls)
            {
                if (!string.IsNullOrEmpty(c.ID))
                {
                    if (c.ID.ToLower().Contains("label"))
                    {
                        lblMessage.Text += ((Label)c).Text + ":";
                    }
                    if (c.ID.ToLower().Contains("text"))
                    {
                        if(!string.IsNullOrEmpty(((TextBox)c).Text))
                        {
                            lblMessage.Text += ((TextBox)c).Text + ",";
                        }
                    }
                }
            }
    
        }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 31, 2012 3:29 AM

All replies

  • User-179079940 posted

    txt=new textbox(); this object to define globally

    i hope solve this issue

    Sunday, October 21, 2012 7:07 AM
  • User-1874889200 posted

    Hi, thanks for the reply. I have defined it globally like this:

     public TextBox txt;

    and then have initialized it like this inside the for loop:

    txt = new TextBox();

    Monday, October 22, 2012 12:02 AM
  • Monday, October 22, 2012 2:07 AM
  • User-1874889200 posted

    Hi, thanks, I have been to this blog but hasn't helped me because i want to know where's the problem with this code i posted..

    Monday, October 22, 2012 3:11 AM
  • User1495804863 posted

    Hi,

    Here is the problem in your code, you assigned some ID to the textboxes, but you not using it to get the values, try like the following code

    protected void addCategoryBtn_Click(object sender, EventArgs e)
        {
           
       for (int i = 0; i <= numControls - 1; i++)
            {
                lblmsg.Text = "TextBox" + i.ToString().Text;
            }
    
        }
    
    

    Monday, October 22, 2012 3:16 AM
  • User-1874889200 posted

    but

    i.ToString() doesn't support the Text property :/ if i use it without text, it prints the ids of the textboxes.
    Monday, October 22, 2012 4:30 AM
  • User-1874889200 posted

    I think there's something wrong with the viewstate which i cannot catch.. :/

    Monday, October 22, 2012 4:42 AM
  • User-735357247 posted

    Hi Seema_naz,

    Please find the below code for your requirement. You may have to remove the last comma from the label, which i have not given in the code. You can always perform what ever you want once you get your dynamic controls information using the below code

     foreach (Control c in Panel1.Controls)
            {
                if (!string.IsNullOrEmpty(c.ID))
                {
                    if (c.ID.ToLower().Contains("label"))
                    {
                        lblMessage.Text += ((Label)c).Text + ":";
                    }
                    if (c.ID.ToLower().Contains("text"))
                    {
                        if(!string.IsNullOrEmpty(((TextBox)c).Text))
                        {
                            lblMessage.Text += ((TextBox)c).Text + ",";
                        }
                    }
                }
            }



    Monday, October 22, 2012 6:04 AM
  • User-1874889200 posted

    Thank you so much for your code, i will try it and let you know :))

    Friday, October 26, 2012 1:35 PM
  • User-1874889200 posted

    I just checked the code, the texts in the labels are captured but not the text i write in the dynamic textboxes.. when i click on the save button, then the texts written in the dynamic textboxes are removed but not the texts in the labels. Therefore, the text of labels are captured and not the textboxes :/ any solution to that?? i think iam one step ahead with your code thanks :))

    Friday, October 26, 2012 1:49 PM
  • User-227014018 posted

    You are right, the problem is in viewstate. Whenever you are creating the textboxes dynamically, create it in Page_Init( ) event, rather than Page_Load( ) event. Actually Viewstate works before Page_Load( ) but after Page_Init( ). 

    you can put the values in the textboxes in the Page_Load( ) event.

    Let me know whether it worked or not.

    With best wishes.

    Saturday, October 27, 2012 5:35 AM
  • User-513218303 posted

    Try in this way,

    TextBox txtTemp=(TextBox) pnlDynamic.FindControl(txt.ID.ToString());

    OR

    TextBox txtTemp=(pnlDynamic.FindControl(txt.ID.ToString())) as TextBox;

     

    But Make sure you are going to find for each iteration means for all dynamically generated TextBoxes 

    Saturday, October 27, 2012 2:33 PM
  • User-1874889200 posted

    hi, and thanks for replies :)

    well my scenario is different :/ i am creating the dynamic textboxes on indexChanged event of a dropdownlist :/ whenever i click one of the values of the dropdown (eg. 2) then two textboxes appear.. how can i do this in page_init??

    and Sujeet Saste, i have tried it but doesn't work.. :/

    Tuesday, October 30, 2012 12:15 AM
  • User-735357247 posted

    When you are creating the textbox dynamic controls try setting the enable view state property and check once

    txt.EnableViewState = true;

    Wednesday, October 31, 2012 3:24 AM
  • User-735357247 posted

    Actually speaking it has to retrieve the text of the textboxes as well. I have tried the similar code and it is absolutely working fine.

    Do one thing. You just create a new solution and paste the below code and let me know whether it is working or not?

    protected void Page_Load(object sender, EventArgs e)
        {
            Label lbl;
            TextBox txt;
            for (int i = 0; i <= 3; i++)
            {
                lbl = new Label();
                txt = new TextBox();
                lbl.Text = "Label" + (i).ToString() + " ";
    
                // txt.Text = "TextBox" + (i).ToString();
    
                lbl.ID = "Label" + i.ToString();
    
                txt.ID = "TextBox" + i.ToString();
                txt.EnableViewState = true;
    
                Panel1.Controls.Add(lbl);
                Panel1.Controls.Add(txt);
    
                //_textBoxes.Add(txt);
    
                Panel1.Controls.Add(new LiteralControl("<br /><br />"));
            }
            //divText.InnerText
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            foreach (Control c in Panel1.Controls)
            {
                if (!string.IsNullOrEmpty(c.ID))
                {
                    if (c.ID.ToLower().Contains("label"))
                    {
                        lblMessage.Text += ((Label)c).Text + ":";
                    }
                    if (c.ID.ToLower().Contains("text"))
                    {
                        if(!string.IsNullOrEmpty(((TextBox)c).Text))
                        {
                            lblMessage.Text += ((TextBox)c).Text + ",";
                        }
                    }
                }
            }
    
        }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 31, 2012 3:29 AM