Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate table and maintaining data across postback problem

Status
Not open for further replies.

jgd1234567

Programmer
May 2, 2007
68
GB
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.
 
Session state would solve your issue.

Why is the property set after page load. If you want the data to be displayed on creation, this is where I would place the table population.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top