Inserting Dynamic Text Box Data Into A Database

Generally Every one can create daynamic text box and they want to store the text which has been entered in to the textbox by the user and they feel its very hard to read the data from the text boxes which are created dynamically.
This blog explains you how to insert daynamic text box data into a SQL Server database.
here I have text boxes that are being generated dynamically . text boxes will be generated while user clicks on Add new Row button and as well as textbox will be removed when the user click on the remove Row button. and I have explained each of those values inserted into the database that hold the results.
The Text boxs will be generated number of times. Here I've wrote you a sample code. It can dynamcially create rows of textboxes and add those values into database. I hope my example will help you to figure out what you should do next.

i have took one hidden control to hold the number of dynamic controls. and i am creating the text box befor the page is post backing. Client side javascript call will help to to increase the
hidden feild value. place below java script in header tag. (click on the code to view enlarged..)


once increment script done. creat a hiddent control with a value as "0" (zero) . and create a asp.net table to hold the dynamic controls. as well s create a button to call the client side script to add new row. and creat one more button to save the data which has entered in to dynamic controls. (click on the code to view enlarged..)




Once we added the controls we need to add the code snippet to create the dynamic controls for each and every time when the user click on the Add new Row button. here if we write the code in page is post back the dynamic controlls will not create for each client side click . to hold the the control view state and creating the dynamic controll i am writing the code in Page Load event.


if (HiddenField1.Value != "")
{
rows = int.Parse(HiddenField1.Value);
for (int i = 0; i <>

{

TableRow tr = new TableRow();

TableCell tc = new TableCell();

TextBox tb = new TextBox();

tb.ID = "FORM" + i.ToString();

tc.Controls.Add(tb);

CheckBox tb2 = new CheckBox();

tb2.Text = "Check if it is a Report";

tb2.ID = "RChk" + i.ToString();

TableCell tc2 = new TableCell();

tc2.Controls.Add(tb2);

tr.Cells.Add(tc); tr.Cells.Add(tc2);

Table1.Rows.Add(tr);

}

if (rows <>

{

btnRemove.Visible = false;

frmname.Visible = false;

}

else

{

frmname.Visible = true; btnRemove.Visible = true;

}

}

here i am making the form name Title visible true only when atleast one row is crated.

How to Retrive the dynamic Control data:

So far we have seen how the dynamic controll will be created while user clicks on a button controll now i am going to give a clear picture how to retive the data from the dynamic control.

as we have took hidden control and assigend the number for ID as prefix of the perticular control.

so using a small for loop we can get back the perticular dynamic controll data. for more inoformation look below code.


for (int i = 0; i <>

{

TextBox Form = Table1.Rows[i].Cells[0].FindControl("FORM" + i.ToString()) as TextBox;

CheckBox F_type = Table1.Rows[i].Cells[1].FindControl("RChk" + i.ToString()) as CheckBox;

if (F_type.Checked)

{

string _F_Type= "REPORT Checked";

}

string _Form_name = Form.Text;

if (Form.Text.Length > 0)
{
result = SaveFormName(_F_Type,_Form_name);
}
}
HiddenField1.Value = Convert.ToString(0);
Table1.Rows.Clear();

Here Save formname will have the dyanamic control data as function arguments to be saved in data base. Hope you enjoyed with the above code.

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites