Aymen Kareem Aymen Kareem - 1 month ago 7
ASP.NET (C#) Question

How to Create Dynamically Generated TextBoxes in ASP.NET

I have this code which generates

TextBoxes
in a Table dynamically, I got it from a website:

protected void AddStdBtn_Click(object sender, EventArgs e)
{
CreateDynamicTable();
}

private void CreateDynamicTable()
{

// Fetch the number of Rows and Columns for the table
// using the properties
if (ViewState["Stu"] != null)
Students = (DataTable)ViewState["Stu"];
int tblCols = 1;
int tblRows = Int32.Parse(NoTxt.Text);
// Now iterate through the table and add your controls
for (int i = 0; i < tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < tblCols; j++)
{
TableCell tc = new TableCell();
TextBox txtBox = new TextBox();
txtBox.ID = "txt-" + i.ToString() + "-" + j.ToString();
txtBox.Text = "RowNo:" + i + " " + "ColumnNo:" + " " + j;
// Add the control to the TableCell
tc.Controls.Add(txtBox);
// Add the TableCell to the TableRow
tr.Cells.Add(tc);

}
// Add the TableRow to the Table
tbl.Rows.Add(tr);
tbl.EnableViewState = true;
ViewState["tbl"] = true;
}
}

protected void CalculateBtn_Click(object sender, EventArgs e)
{
foreach (TableRow tr in tbl.Controls)
{
foreach (TableCell tc in tr.Controls)
{

if (tc.Controls[0] is TextBox)
{
Response.Write(((TextBox)tc.Controls[0]).Text);
}
}
Response.Write("<br/>");
}
}


protected override object SaveViewState()
{
object[] newViewState = new object[2];

List<string> txtValues = new List<string>();

foreach (TableRow row in tbl.Controls)
{
foreach (TableCell cell in row.Controls)
{
if (cell.Controls[0] is TextBox)
{
txtValues.Add(((TextBox)cell.Controls[0]).Text);
}
}
}

newViewState[0] = txtValues.ToArray();
newViewState[1] = base.SaveViewState();
return newViewState;
}
protected override void LoadViewState(object savedState)
{
//if we can identify the custom view state as defined in the override for SaveViewState
if (savedState is object[] && ((object[])savedState).Length == 2 && ((object[])savedState)[0] is string[])
{
object[] newViewState = (object[])savedState;
string[] txtValues = (string[])(newViewState[0]);

if (ViewState["Stu"] != null)
Students = (DataTable)ViewState["Stu"];

if (txtValues.Length > 0)
{
//re-load tables
CreateDynamicTable();
int i = 0;
foreach (TableRow row in tbl.Controls)
{
foreach (TableCell cell in row.Controls)
{
if (cell.Controls[0] is TextBox && i < txtValues.Length)
{
((TextBox)cell.Controls[0]).Text = txtValues[i++].ToString();

}
}
}
}
//load the ViewState normally
base.LoadViewState(newViewState[1]);
}
else
{
base.LoadViewState(savedState);
}
}


Now if you examine CreateDynamicTable() function, particularly the line:

int tblRows = Int32.Parse(NoTxt.Text);


you can see that the user can control the number of rows by entering a number in the
TextBox
NoTxt
. When I run this code I get the following error:

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

Additional information: Input string was not in a correct format.


and the error location is in the same line above.

The question is: How can I let the user control the number of rows in the table?

Answer

After few days of reading and research, the solution can be done by using Session variables. Store the value obtained in NoTxt.Text as follows for example:

Session["V"] = Int32.Parse(NoTxt.Text); 

and retrieve it once you need it.