in this code some problems are there please verify and give the solution:
(1). While the page is postBack it generating one more text box which we does n't want.
(2). How to store the Dynamic texbox values in the Database.
(3). the values of each texbox should store in each cells of the table
code:
public partial class Addtextbox : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=ESPOSTIO-5A065C;Initial Catalog=Elance;Integrated Security=sspi");
SqlCommand cmd;
protected void Page_Load(object sender, EventArgs e)
{
}
private void CreateTextBoxes()
{
for (int counter = 0; counter < NumberOfControls; counter++)
{
TextBox tb = new TextBox();
tb.Width = 150;
tb.Height = 18;
tb.TextMode = TextBoxMode.SingleLine;
tb.ID = "TextBoxID" + (counter + 1).ToString();
// add some dummy data to textboxes
tb.Text = " ";
phTextBoxes.Controls.Add(tb);
phTextBoxes.Controls.Add(new LiteralControl("<br/> <br/>"));
}
}
protected override void CreateChildControls()
{
// Here we are recreating controls to persist the ViewState on every post back
if (Page.IsPostBack)
{
NumberOfControls += 0;
CreateTextBoxes();
}
else
{
CreateTextBoxes();
// Increase the control value to 1
NumberOfControls = 1;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
NumberOfControls += 1;
}
public int NumberOfControls
{
get
{
if (ViewState["Count"] == null)
{
return 0;
}
return (int)ViewState["Count"];
}
set
{
ViewState["Count"] = value++;
}
}
private void ReadTextBoxes()
{
string strValue;
strValue = string.Empty;
int n = NumberOfControls;
for (int i = 0; i < NumberOfControls; i++)
{
string boxName = "TextBoxID" + (i+1).ToString();
TextBox tb = phTextBoxes.FindControl(boxName) as TextBox;
strValue += tb.Text + ",";
}
Response.Write(strValue);
}
protected void Button2_Click(object sender, EventArgs e)
{
//here we required to store in database.
ReadTextBoxes();
}
}