动态添加文本框并在ASP.NET中的文本框中保存数据
问题描述:
i m trying to add textbox dynamically at runtime and saving data in sql server. i am able generate textbox but not able send data in database and it is not giving any error also. what i am doing wrong? here is my code
What I have tried:
protected void btnAddCtrl_Click(object sender, EventArgs e)
{
int rowCount = 0;
//initialize a session.
rowCount = Convert.ToInt32(Session["clicks"]);
rowCount++;
//In each button clic save the numbers into the session.
Session["clicks"] = rowCount;
//Create the textboxes and labels each time the button is clicked.
for (int i = 0; i < rowCount; i++)
{
TextBox TxtBoxU = new TextBox();
//TextBox TxtBoxE = new TextBox();
Label lblU = new Label();
// Label lblE = new Label();
TxtBoxU.ID = "TextBoxU" + i.ToString();
// TxtBoxE.ID = "TextBoxE" + i.ToString();
lblU.ID = "LabelU" + i.ToString();
// lblE.ID = "LabelE" + i.ToString();
lblU.Text = "User " + (i + 1).ToString() + " : ";
// lblE.Text = "E-Mail : ";
//Add the labels and textboxes to the Panel.
Panel1.Controls.Add(lblU);
Panel1.Controls.Add(TxtBoxU);
Panel1.Controls.Add(new LiteralControl("<br />"));
Panel1.Controls.Add(new LiteralControl("<br />"));
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (TextBox textBox in Panel1.Controls.OfType<textbox>())
{
string constr = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO AssetsDetail (FieldName) VALUES(@FieldName)",con))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@FieldName", textBox.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}</textbox>
答
试试这个。
受到 ASP.Net的启发持久动态控件:动态控件在ASP.Net中的PostBack后消失 [ ^ ]
try this.
inspired from ASP.Net Persist Dynamic Controls: Dynamic Controls disappear after PostBack in ASP.Net[^]
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAddCtrl_Click(object sender, EventArgs e)
{
int rowCount = 0;
rowCount = Convert.ToInt32(Session["clicks"]);
rowCount++;
Session["clicks"] = rowCount;
int i = rowCount;
TextBox TxtBoxU = new TextBox();
Label lblU = new Label();
TxtBoxU.ID = "TextBoxU" + i.ToString();
lblU.ID = "LabelU" + i.ToString();
lblU.Text = "User " + i.ToString() + " : ";
Panel1.Controls.Add(lblU);
Panel1.Controls.Add(TxtBoxU);
}
protected void Page_PreInit(object sender, EventArgs e)
{
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("TextBoxU")).ToList();
foreach (string key in keys)
{
string id = key.Replace("TextBoxU", "");
this.CreateTextBox(key, "LabelU" + id , id);
}
}
private void CreateTextBox(string id, string labelId,string idNo )
{
Label lbl = new Label();
TextBox txt = new TextBox();
txt.ID = id;
lbl.ID = labelId;
lbl.Text = "User " + idNo + " : ";
Panel1.Controls.Add(lbl);
Panel1.Controls.Add(txt);
}
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (TextBox textBox in Panel1.Controls.OfType<TextBox>())
{
string text = textBox.Text;
// your code to insert into database
}
}
}