Create Dynamic Table on Clicking Button in ASP.NET

Problem: If we create dynamic table and bind table on one button click then can't be accessed value in another button because of viewstate lose.

Solution:

create the dynamic table as shown below. The code has been commented to help you understand.
C#

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


private void CreateDynamicTable()
{
PlaceHolder1.Controls.Clear();

// Fetch the number of Rows and Columns for the table
// using the properties
int tblRows = Rows;
int tblCols = Columns;
// Create a Table and set its properties
Table tbl = new Table();
// Add the table to the placeholder control
PlaceHolder1.Controls.Add(tbl);
// 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.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);
}

// This parameter helps determine in the LoadViewState event,
// whether to recreate the dynamic controls or not

ViewState["dynamictable"] = true;
}

VB.NET

Protected Sub btnGenerate_Click(ByVal sender As Object, ByVal e As EventArgs)
CreateDynamicTable()
End Sub


Private Sub CreateDynamicTable()
PlaceHolder1.Controls.Clear()

' Fetch the number of Rows and Columns for the table
' using the properties
Dim tblRows As Integer = Rows
Dim tblCols As Integer = Columns
' Create a Table and set its properties
Dim tbl As Table = New Table()
' Add the table to the placeholder control
PlaceHolder1.Controls.Add(tbl)
' Now iterate through the table and add your controls
For i As Integer = 0 To tblRows - 1
Dim tr As TableRow = New TableRow()
For j As Integer = 0 To tblCols - 1
Dim tc As TableCell = New TableCell()
Dim txtBox As TextBox = New TextBox()
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)
Next j
' Add the TableRow to the Table
tbl.Rows.Add(tr)
Next i

' This parameter helps determine in the LoadViewState event,
' whether to recreate the dynamic controls or not

ViewState("dynamictable") = True
End Sub

Step 2: The last piece of code is to determine whether to recreate the controls based on the ViewState[“dynamictable”]. Here’s how it works. The first time the page loads, the LoadViewState() does not execute. The ViewState[“dynamictable”] flag is initialized once the button click occurs. When the postback occurs on the second button click (btnPost), it is then that our code written for overriding the LoadViewState() runs. The base.LoadViewState() instantiates the ViewState object. We then check the value of our ViewState flag, and based on the result, re-create our Table.
C#


// Check the ViewState flag to determine whether to
// rebuild your table again
protected override void LoadViewState(object earlierState)
{
base.LoadViewState(earlierState);
if (ViewState["dynamictable"] == null)
CreateDynamicTable();
}


VB.NET


' Check the ViewState flag to determine whether to
' rebuild your table again
Protected Overrides Sub LoadViewState(ByVal earlierState As Object)
MyBase.LoadViewState(earlierState)
If ViewState("dynamictable") Is Nothing Then
CreateDynamicTable()
End If
End Sub


That’s it. Run the code. Enter the number of Rows and Columns to be created and click the Generate button to create the table. Since the table contains textboxes in each cell, manipulate the text inside the table and hit the ‘Cause Postback’ button. Even after the postback, the values of the table control are retained.
Note: If multiple controls of the same type are created, remember to create them with the same ID's.

No comments:

Post a Comment