Click here to Skip to main content
15,893,663 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am Creating A survey Site :

I want to add the textboxes dynamically and then get there values in the database.

now let say I select 4 textboxes seslected from the dropdown to be there Dynamically

Code on th Selection on dropdown :

protected void NumDropDown_SelectedIndexChanged(object sender, EventArgs e)
   {
       if (DropDownList1.SelectedValue == "TextBox")
       {

           int j;
           i = int.Parse(NumDropDown.SelectedValue);
           Session["i"] = i;
           switch (i)
           {
               case 1:
                   t = new TextBox[i];
                   Session["textBox"] = t;
                   for (j = 0; j < i; j++)
                   {
                       t[j] = new TextBox();
                       t[j].ID = "txtCheckbox" + j.ToString();
                       Panel1.Controls.Add(t[j]);

                   }
                   break;
               case 2:
                   t = new TextBox[i];
                   Session["textBox"] = t;
                   for (j = 0; j < i; j++)
                   {
                       t[j] = new TextBox();
                       t[j].ID = "txtCheckbox" + j.ToString();
                       Panel1.Controls.Add(t[j]);

                   }
                   break;

               case 3:
                   t = new TextBox[i];
                   Session["textBox"] = t;
                   for (j = 0; j < i; j++)
                   {
                       t[j] = new TextBox();
                       t[j].ID = "txtCheckbox" + j.ToString();
                       Panel1.Controls.Add(t[j]);

                   }
                   break;
               case 4:
                   t = new TextBox[i];
                   List<TextBox> MyTextBoxes;
                   for (j = 0; j < i; j++)
                   {
                       t[j] = new TextBox();
                       t[j].ID = "txtCheckbox" + j.ToString();
                       Panel1.Controls.Add(t[j]);
                       try
                       {
                           MyTextBoxes = (List<TextBox>)Session["AddedTextBox"];
                           MyTextBoxes.Add(t[j]);
                           Session["AddedTextBox"] = MyTextBoxes;
                       }
                       catch
                       {
                           MyTextBoxes = new List<TextBox>();
                           MyTextBoxes.Add(t[j]);
                           Session["AddedTextBox"] = MyTextBoxes;
                       }
                   }
                   break;
           }

       }
   }

2) Then Here I entered the values in the textBox like a, b,c,d and click ADD:

Code for the click On the ADD Click :

1) First i checked the session to be there on Page_Init:

C#
protected void Page_Init(object sender, EventArgs e) { if (Session["AddedTextBox"] != null) { string a; string b; string c; string d;

            int listCount = ((List<TextBox>)Session["AddedTextBox"]).Count;
            foreach (TextBox t in ((List<TextBox>)Session["AddedTextBox"]))
            {
                if (listCount == 1)
                {

                }
                if (listCount == 2)
                {

                }
                if (listCount == 3)
                {

                }
                if (listCount == 4)
                {
                    if (t.ID == "txtCheckbox0")
                    {
                        a = t.Text;
                    }
                    if (t.ID == "txtCheckbox0")
                    {
                        b = t.Text;
                    }
                    if (t.ID == "txtCheckbox0")
                    {
                        c = t.Text;
                    }
                    if (t.ID == "txtCheckbox0")
                    {
                        d = t.Text;
                    }

                }
  }
        }




But The prob here is that I dont get the text values, They Appear to b empty. Please help me to solve this issue
Posted
Updated 11-Feb-13 6:15am
v2
Comments
Sergey Alexandrovich Kryukov 11-Feb-13 12:00pm    
The code it too bad to discuss it. Look at the long if block with hard-coded IDs! Maybe, explain the idea of survey behavior in more detail...
—SA
xxxMI 11-Feb-13 12:07pm    
Dear sir,

I am new in coding, please help me to improve the same and how to implement it in best practice.

Please can you take the remote control of my pc so that i can explain you my question more clearly..


Richard C Bishop 11-Feb-13 12:16pm    
We don't take remote control of user's computers here. Can you imagine the security risk that would pose?
Sergey Alexandrovich Kryukov 11-Feb-13 12:35pm    
The ability to explain issue is a part of your profession. And if the problem looks hopeless even at this stage, you could turn to some simpler task...
—SA

1 solution

Hi dear

first of all i think you no need to check session on Page_Int yes if you are creating session in another page then valid for same page why you are creating session.
Please look following code it's working fine.

C#
protected void drp_SelectedIndexChanged(object sender, EventArgs e)
        {
            CreateTextBox();
        }


C#
private void CreateTextBox()
        {
            for (int i = 0; i < Convert.ToInt32(this.drp.SelectedValue); i++)
            {
                TextBox txtBox = new TextBox();
                txtBox.ID = "Txt" + Convert.ToString(i + 1);
                txtBox.Text = "TextBox" + Convert.ToString(i + 1);
                this.Panel1.Controls.Add(txtBox);
            }
        }


C#
protected void btn_Click(object sender, EventArgs e)
        {
            GetValues();
        }


private void GetValues()
        {
            this.Panel1.Controls.Clear();
            CreateTextBox();
            StringBuilder objBuilder = new StringBuilder();
            string Id = "";
            for (int i = 0; i < Convert.ToInt32(this.drp.SelectedValue); i++)
            {
                Id = "Txt" + Convert.ToString(i + 1);
                TextBox txtBox = ((TextBox)this.Page.FindControl(Id));
                if (txtBox != null)
                {
                    objBuilder.Append("TextBox" + Convert.ToString(i + 1) + " Value = " + txtBox.Text);
                }
            }
        }


You will calll CreateTextBox() method again because it's dynamically adding Controls adding on PostBack they'll remove you just check this code you will get all values.

[Edit: Removed begging for vote]
Thanks Best of luck.
 
Share this answer
 
v2
Comments
Maciej Los 13-Feb-13 13:36pm    
Good job, +5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900