Friday, August 18, 2006

Programmatically Creating rows in Table in ASP.net

I had been trying to figure out how to add rows to a table programmatically in ASP.net. The adding of rows I believe is very simple, the problem comes when you also need to use the controls added in the row, to get the values entered by the user and do further processing. I needed to have a functionality similar to the yahoo mail attachments where you click on add, and voila you get a new row for attachment.

The code below show you how to achieve this.

I have added a Textbox, file upload control and a Checkbox to the dynamically added row. You will have to add a placeholder with id "Main_table" to the form. This is where the table with your controls is added. The createrow will have to be called from the click event of the 'ADD ROW' button.


Protected Sub CreateRow()

num_row = (main_table.Rows).Count

Dim r As TableRow = New TableRow
Dim c1 As TableCell = New TableCell
Dim c2 As TableCell = New TableCell
Dim c3 As TableCell = New TableCell

r.ID = "newRow" & num_row
c1.ID = "newCol1" & num_row
c1.Style.Add("width", "30%")
c1.Style.Add("TEXT-ALIGN", "left")

c2.ID = "newCol2" & num_row
c2.Style.Add("width", "30%")
c2.Style.Add("TEXT-ALIGN", "left")

c3.ID = "newCol3" & num_row
c3.Style.Add("TEXT-ALIGN", "left")

Dim t As TextBox = New TextBox
t.ID = "tDocument" & num_row
t.Style.Add("width", "190px")
t.EnableViewState = True

Dim f As FileUpload = New FileUpload
f.ID = "fTemplate" & num_row
f.CssClass = "Fileupload"
f.EnableViewState = True

Dim cb As CheckBox = New CheckBox
cb.ID = "cRequired" & num_row
cb.EnableViewState = True
cb.Checked = True

c1.Controls.Add(t)
c2.Controls.Add(cb)
c3.Controls.Add(f)

r.Cells.Add(c1)
r.Cells.Add(c2)
r.Cells.Add(c3)

main_table.Rows.Add(r)

num_row = num_row + 1
Session("table") = main_table
End Sub



To access the element values on submitting the form,
you could use the following code.



For i = 0 To main_table.Rows.Count - 1
arr_docnames.Add( _
CType(main_table.FindControl("tDocument" & i), TextBox).Text)
If CType(main_table.FindControl("cRequired" & i), _
CheckBox).Checked = True Then
arr_approvals.Add(1)
Else
arr_approvals.Add(0)
End If

If CType(main_table.FindControl("fTemplate" & i), _
FileUpload).PostedFile Is Nothing Then
arr_templates.Add("")
arr_physicalfilenames.Add("")
Else
arr_templates.Add(_
CType(main_table.FindControl("fTemplate" & i), _
FileUpload).PostedFile.InputStream)
arr_physicalfilenames.Add( _
CType(main_table.FindControl("fTemplate" & i), _
FileUpload).FileName)
End If

'AttachmentFile.PostedFile.InputStream
Next


Here I have declared the arr_templates, arr_physicalfilename, arr_approvals as arraylists to store the values that have been posted via the form. This way we do not need to worry about how many rows were added, the for loop will take care of this for us. We then need to convert the the object to the type expected by us and call the property to retrieve the values. In the above example I was trying to retrieve the data and save the binary data from the file to database.

The code above is given in VB.net, You might have to use the VB.net to C# converter in case you are a big fan of C#.

No comments: