Hi akshayes004,
Refer below code.
HTML
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="AddTextBox" />
<asp:Panel ID="pnlTextBoxes" runat="server"></asp:Panel>
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="SaveAll" />
Code
protected void Page_PreInit(object sender, EventArgs e)
{
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
for (int i = 1; i <= keys.Count; i++)
{
this.CreateTextBox("txtDynamic" + i);
i++;
}
}
protected void AddTextBox(object sender, EventArgs e)
{
int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
this.CreateTextBox("txtDynamic" + index);
}
protected void SaveAll(object sender, EventArgs e)
{
for (int i = 0; i < pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count; i++)
{
string textuom = pnlTextBoxes.Controls.OfType<TextBox>().ToList()[i].Text.Trim();
string textitem = pnlTextBoxes.Controls.OfType<TextBox>().ToList()[i + 1].Text.Trim();
i++;
var X = 1;
// You code for saving the TextBox value.
string constr = ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO GATETABLE (NAME,ID,ITEMs,DEPARTMENT,IS_ACTIVE,CREATION_DATE,RECORD_ID,ITEM_UOM) VALUES (@NAME, @ID,@ITEMs,@DEPARTMENT,@IS_ACTIVE,@CREATION_DATE,@RECORD_ID,@ITEM_UOM)"))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Name", txt_name.Text.ToString());
cmd.Parameters.AddWithValue("@ID", txt_ID.Text.ToString());
cmd.Parameters.AddWithValue("@ITEMs", textitem);
cmd.Parameters.AddWithValue("@DEPARTMENT", txtdept.Text.ToString());
cmd.Parameters.AddWithValue("@CREATION_DATE", dateToday);
cmd.Parameters.AddWithValue("@IS_ACTIVE", X);
cmd.Parameters.AddWithValue("@RECORD_ID", recordId);
cmd.Parameters.AddWithValue("@ITEM_UOM", textuom);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
ScriptManager.RegisterStartupScript(this, this.GetType(), "redirect", "alert('DETAILS SAVED SUCCESSFULLY'); window.location='" + Request.ApplicationPath + "textbox.aspx';", true);
}
}
}
}
private void CreateTextBox(string id)
{
Literal ltuom = new Literal();
ltuom.Text = "UOM : ";
pnlTextBoxes.Controls.Add(ltuom);
TextBox txtuom = new TextBox();
txtuom.ID = id;
pnlTextBoxes.Controls.Add(txtuom);
id = "txtDynamic" + (Convert.ToInt32(id.Replace("txtDynamic", "")) + 1);
Literal ltitem = new Literal();
ltitem.Text = "ITEM : ";
pnlTextBoxes.Controls.Add(ltitem);
TextBox txtitem = new TextBox();
txtitem.ID = id;
pnlTextBoxes.Controls.Add(txtitem);
Literal lt = new Literal();
lt.Text = "<br />";
pnlTextBoxes.Controls.Add(lt);
}
Screenshot