I am having a problem with an C# ASP.NET form. The purpose of the form is to load some key/value pairs from a database to allow a user to view, edit or delete them.
The Key/Value pairs are dynamically generated in the Page_Load event, and everything works well when the page is first openend. Here is some example code (trimmed down from the original)
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Text = keyName;
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = keyValue;
row.Cells.Add(cell);
KeyValueTable.Rows.Add(row);
I just loop through all of the Key/Value pairs and each are added dynamically to a table. At the point of page loading, everything is running smoothly, and all Key/Value pairs show as expected.
The problem occurs when a user clicks a button, which since is set to runat="server", generates a post back event. What this does is it appears to do is clear the dynamic content generated above, and then recreates it (since it is created in the Page_Load event).
What I would prefer to do is choose when that dynamic content is cleared, since I don't want it refreshing unnecessarily, since each refresh is actually occurring from a database.
What I have attempted to do is perform the generation of the dynamic content if it isn't a postback. I.E:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GenerateDynamicContent();
}
}
However what that now does is generate the dynamic content when the page first loads, however as soon as some postback event occurs (such as a user clicking a button), the dynamic content is wiped, but not created.
Basically what I am hoping for is to discover some way where I can create dynamic content, as shown above, that isn't wiped on any postback event. I want to be able to control when it is refreshed, rather than it occurring on any postback event. Is this possible?
Many thanks
I've found that putting the dynamic code into Page_Init instead of Page_Load prevents the content from being wiped during a postback