dynamically adding textboxes when clicking the button

Posted by Srinunelli under ASP.NET on 2/6/2012 | Points: 10 | Views : 1885 | Status : [Member] | Replies : 1
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();
}
}




Responses

Posted by: Blessyjees on: 2/6/2012 [Member] Bronze | Points: 25

Up
0
Down
hi,

You can do this in a simple way. Try this

 public partial class WebForm1 : System.Web.UI.Page

{
protected void Page_Load(object sender, EventArgs e)
{
for (int counter = 0; counter < 5; 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 = " ";
this.phControl.Controls.Add(tb);
this.phControl.Controls.Add(new LiteralControl("<br/> <br/>"));
}
}

protected void btnsave_Click(object sender, EventArgs e)
{
string strValue = string.Empty;
int count = this.phControl.Controls.Count;
for (int index =0; index < count; index++)
{
if (this.phControl.Controls[index].GetType() == typeof(TextBox))
{
TextBox textbox = (TextBox)this.phControl.Controls[index];
strValue = strValue + textbox.Text;
}
}
Response.Write(strValue);
}
}


Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Srinunelli, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response