Create a Table Dynamically in ASP.NET 2.0 and ASP.NET 3.5
I had recently received a request from a dotnetcurry.com visitor about creating a dynamic table in ASP.NET. Now there are plenty of solutions available on the net. Most of the solutions I found, advised creating a dynamic table in the Page_Init() event, since the values of controls already exist in this event and can be used after a postback occurs. However this solution works best when the number of rows and columns are fixed, and then the table is created. But what if the number of rows and columns to be created, are to be accepted from the user, at runtime? In this article, we will explore how to accept the number of rows and columns from the user, create the table in the Page_Load() event and also retain the values on postback.
Whenever a control is added to the page dynamically, it is not persisted by default. That is because these ‘dynamically’ added controls are not automatically added to the page view state. Remember that for a dynamic control, the viewstate is available only after the post back occurs.
To elaborate on the above statement, the page is recreated each time it is posted back to the server. In other words, a new instance of the Page class is created and the class variables are set using the values from the ViewState. However during this recreation, the dynamically created controls are no longer available and hence the values are lost in the viewstate.
To override this behavior, you need to somehow make these controls available on each postback. One way of doing so, is to override the LoadViewState() method of the page. In the LoadViewState(), the view state data that had been saved from the previous page visit is loaded and recursively populated into the control hierarchy of the Page.
Why the LoadViewState()?
When the controls are added to page, it is done quiet early in the page event cycle. Hence the LoadViewState() gives you an ideal placeholder to recreate the controls. Since the LoadViewState() method is called before the Page_Load() event, re-adding controls in this method assures that the controls can be access and manipulated by the time any event occurs on them.
Why have you chosen to create and recreate the controls on Page_Load() instead of Page_Init()?
You can create dynamic controls in either the Page_Init() or Page_Load().
However as already explained in the introduction, we would be accepting the rows and columns from the user to create the table. Since these values would be available only after the user has entered the values in the two textboxes and clicked the button to cause a postback, the best suitable place is the Page_Load().
The second reason of choosing Page_Load() over Page_Init() is that in this example, we will be setting a flag on the ViewState to determine if the dynamic table has to be recreated or not. ViewState values are available during the Page_Load() and ‘not’ during the Page_Init() event.
Let us see some code now.
Step 1: Create a new ASP.NET application. Add two textboxes (txtRows and txtCols) and a button (btnGenerate) control to the page. The two textboxes will accept the number of Rows and Columns from the user. Based on the input in the textboxes, the table will be created dynamically on the button click. Also add a container element (to position the table) and a button (btnPost) to cause a postback the second time, once the table data is manipulated.
The mark up will look similar to the following:
Tuesday, June 3, 2008
Download .NET Framework 3.5
Try It
Microsoft .NET Framework 3.5 is available for download.
Download the Current .NET Framework
Download .NET Framework 3.5
Previous versions of .NET Framework are also available for download:
Download Previous Versions
.NET Framework 3.0
.NET Framework 2.0
.NET Framework 1.1
Subscribe to:
Posts (Atom)