Ich erstelle eine Umfrage-Site. Ich möchte die Textfelder dynamisch hinzufügen und dann ihre Werte in der Datenbank abrufen.Wie Sie Textfelder dynamisch mit ASP.NET erstellen und dann ihre Werte in der Datenbank speichern?
Jetzt sagen wir, dass ich 4 Textfelder aus dem Dropdown auswählen, um dynamisch da zu sein.
-Code auf der Auswahl auf Drop-Down:
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) Dann Hier trat ich die Werte in der Textbox wie a, b, c, d und Hinzufügen klicken:
-Code für Klicken Sie auf die per Klick auf die ADD:
1) Zuerst habe ich die Sitzung geprüft, dort zu sein auf 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;
}
}
}
}
Aber das Problem hier ist, dass ich die Textwerte nicht erhalte, sie scheinen leer zu sein. Bitte helfen Sie mir, dieses Problem zu lösen.
Sie von dem Ansatz, der hier erwähnten profitieren können: [Gibt es eine textboxlist Steuerung verfügbar irgendwo?] (http://stackoverflow.com/questions/9070327/is-there-a -textboxlist-control-available-irgendwo – MikeM