Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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:
 
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 11-Feb-13 5:58am
xxxMI448
Edited 11-Feb-13 6:15am
v2
Comments
Sergey Alexandrovich Kryukov at 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
Pratik Jaiswal at 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..
 

richcb at 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 at 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

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
protected void drp_SelectedIndexChanged(object sender, EventArgs e)
        {
            CreateTextBox();
        }
 
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);
            }
        }
 
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.
  Permalink  
v2
Comments
Maciej Los at 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)

  Print Answers RSS
0 OriginalGriff 400
1 Gihan Liyanage 238
2 Sergey Alexandrovich Kryukov 150
3 ChauhanAjay 120
4 syed shanu 110
0 Sergey Alexandrovich Kryukov 9,021
1 OriginalGriff 7,941
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100