jgd1234567
Programmer
Hi i have the following code within a user control to build a table:
protected int RowCounter
{
get
{
object o = this.ViewState["RowCounter"];
return o != null ? (int)o : 0;
}
set
{
this.ViewState["RowCounter"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
this.RebuildTable();
}
protected void RebuildTable()
{
for (int i = 0; i < this.RowCounter; i++)
{
this.AddRow();
}
}
protected void AddRow()
{
// Create the cell
TableCell cell = new TableCell();
// Add the textbox and spacer to the cell
cell.Controls.Add(new TextBox());
// Create the row and add the cell
TableRow row = new TableRow();
row.Cells.Add(cell);
// Finally add the row to the table
tblScreenNames.Rows.Add(row);
}
protected void btnAddRow_Click(Object sender, EventArgs e)
{
this.AddRow();
this.RowCounter++;
}
When clicking the add row button a new row is added and the remaining values are still populated from the view state. I now wish to pass a collection to populate the table when the page first loads. I added my property but the Page_Load is called before the property is set so I tried using the Page_PreRender method (and rebuild/populate the table when my property is set) but now when you click the button to add a new row the previous entered information is lost (i assume since the pre render happens after the viewstate is populated). I have gone over and over this in my head and it seems that i need to build the table in the Page_Load (to maintain the data across postbacks) but i cannot get the number of rows to add since this is not discovered until after my property is set but this does not happen until after the Page_Load.
It seems i'm going around in circles. I'd really appreciate if someone could help.
protected int RowCounter
{
get
{
object o = this.ViewState["RowCounter"];
return o != null ? (int)o : 0;
}
set
{
this.ViewState["RowCounter"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
this.RebuildTable();
}
protected void RebuildTable()
{
for (int i = 0; i < this.RowCounter; i++)
{
this.AddRow();
}
}
protected void AddRow()
{
// Create the cell
TableCell cell = new TableCell();
// Add the textbox and spacer to the cell
cell.Controls.Add(new TextBox());
// Create the row and add the cell
TableRow row = new TableRow();
row.Cells.Add(cell);
// Finally add the row to the table
tblScreenNames.Rows.Add(row);
}
protected void btnAddRow_Click(Object sender, EventArgs e)
{
this.AddRow();
this.RowCounter++;
}
When clicking the add row button a new row is added and the remaining values are still populated from the view state. I now wish to pass a collection to populate the table when the page first loads. I added my property but the Page_Load is called before the property is set so I tried using the Page_PreRender method (and rebuild/populate the table when my property is set) but now when you click the button to add a new row the previous entered information is lost (i assume since the pre render happens after the viewstate is populated). I have gone over and over this in my head and it seems that i need to build the table in the Page_Load (to maintain the data across postbacks) but i cannot get the number of rows to add since this is not discovered until after my property is set but this does not happen until after the Page_Load.
It seems i'm going around in circles. I'd really appreciate if someone could help.