To get the properties of the controls generated in runtime and placed on the Table control

D

DotNetJunkies User

Hi All

I am developing a site using ASP.Net

I want to develop a site that generates forms ( textbox,check boxes etc) automatically ie is from database itself depending on the no of rows in the table in database. That is to get as many textboxes and checkboxes as there are rows in table

What I am doing right now is creating new text box, checkboxes etc in the code and then adding them to the cells and rows of the table control.
This all works fine.

The problem comes when I have to extract data from the form and insert into databse table.
How do I get the properties of the controls generated in runtime and placed on the Table control?

here are pieces of my code.


Public rows, textCount, chkCount, cmbCount As Integer
Public arr(,) As String //An array to store ids of auto generated controls

//To create forms
Dim locType, forType As String
locType = cmbSelect.SelectedItem.Value
forType = cmbSelect1.SelectedItem.Value

Dim con As New OdbcConnection(ConfigurationSettings.AppSettings("conStr"))
Dim da As New OdbcDataAdapter("select * from TEST_FORMAT where LType='" + locType + "'and FORMATTYPE='" + forType + "' ORDER BY PRINTID", con)

Dim ds As New DataSet()
da.Fill(ds)

Dim i, col As Integer
rows = ds.Tables(0).Rows.Count
ReDim arr(rows, 3)

For i = 0 To rows - 1
Dim row As New TableRow()
Dim cChkBox As New TableCell()
Dim cLab As New TableCell()

Dim cTxtBox As New TableCell()
Dim cPID As New TableCell()


Dim PrintId As New System.Web.UI.WebControls.Label()
Dim l As New System.Web.UI.WebControls.TextBox()
Dim parent, PID As String

PID = ds.Tables(0).Rows(i).Item(0).ToString
arr(i, 0) = PID

parent = ds.Tables(0).Rows(i).Item("PARENTID").ToString

PrintId.Text = ds.Tables(0).Rows(i).Item("PRINTID")
PrintId.BackColor = Color.Cornsilk

cPID.Controls.Add(PrintId)
row.Cells.Add(cPID)

If Not parent = "root" Then //making check box only for sub points and not for main Points
Dim chkBox As New Web.UI.WebControls.CheckBox()
Response.Write(chkBox.GetType.ToString)
chkBox.ID = "chkBox" & i
arr(i, 1) = chkBox.ID
cChkBox.Controls.Add(chkBox)
row.Cells.Add(cChkBox)
chkCount += 1
chkBox.EnableViewState = True

Else //if it is main point no need of check box
cChkBox.Controls.Add(New System.Web.UI.WebControls.Label())
arr(i, 1) = "NULL"
row.Cells.Add(cChkBox)
End If

l.Text = ds.Tables(0).Rows(i).Item("DESCRIPTION")
l.TextMode = TextBoxMode.MultiLine
l.ForeColor = Color.Black
l.Rows = 2
l.Columns = 40
l.ReadOnly = True
l.BorderColor = Color.Empty
cLab.Controls.Add(l)
row.Cells.Add(cLab)

If Not parent = "root" Then
If Ques = 1 Then
Dim txtBox As New Web.UI.WebControls.TextBox()
Response.Write(txtBox.GetType.ToString)
txtBox.TextMode = TextBoxMode.MultiLine
txtBox.Rows = 2
txtBox.Columns = 40
txtBox.ID = "txtBox" & i
arr(i, 2) = txtBox.ID
cTxtBox.BorderStyle = BorderStyle.Inset
cTxtBox.Controls.Add(txtBox)
row.Cells.Add(cTxtBox)
textCount += 1

ElseIf Ques = 2 Then
Dim cmb As New Web.UI.WebControls.DropDownList()
Response.Write(cmb.GetType.ToString)
cmb.Items.Add("YES")
cmb.Items.Add("NO")
cmb.ID = "cmb" & i
arr(i, 2) = cmb.ID
cTxtBox.BorderStyle = BorderStyle.Inset
cTxtBox.Controls.Add(cmb)
row.Cells.Add(cTxtBox)
cmbCount += 1

End If

Else
arr(i, 2) = "NULL"
End If

// To access properties of controls generated at runtime

Function iterateReadControls(ByVal par As Control)
Dim c As Control
Dim var As String
For Each c In par.Controls
var = c.GetType.ToString
Response.Write(c.GetType.ToString)
Response.Write("*")
' If c.GetType.ToString.Equals("System.Web.UI.WebControls.Table") Then
'Dim t As New Table()

If c.Controls.Count > 0 Then
iterateReadControls(c)
End If
If c.GetType.ToString.Equals("System.Web.UI.WebControls.CheckBox") Then
chkCount += 1
ElseIf c.GetType.ToString.Equals("System.Web.UI.WebControls.TextBox") Then
textCount += 1
ElseIf c.GetType.ToString.Equals("System.Web.UI.WebControls.DropDownList") Then
cmbCount += 1
End If
Next
End Function



i would be very grateful if anyone can guide me with this
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top