I am creating a survey site. I want to add the textboxes dynamically and then get their values in the database.
Now let's say I select 4 textboxes from the dropdown to be there dynamically.
Code on the 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 problem here is that I don't get the text values, they Appear to be empty.
Please help me to solve this issue.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…