Loop through Dynamic Controls

mpdillon

Member
Joined
Feb 17, 2005
Messages
20
Location
Glen Mills, PA
Programming Experience
Beginner
I am adding dynamic controls to my web page. I need to loop through them to determine the values entered by the user. What is the correct code to loop through all the controls on a web page. Here is what I have been trying and I will explain why the results are not what I want.



Dim ctl as Control

For Each ctl In XXXXXXX

If ctl.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") Then

‘Do Something

End If

Next



When XXXXXXX is Me.Controls I get values such as System.Web.Ui.LiteralControls, System.Web.UI.HtmlControls.HtlmHead or System.Web.UI.HtmlControls|Form.



When XXXXXXX is Me.form1.Controls I do not get any controls.



The For Each loop never finds a text box. Am I using the incorrect item for XXXXXXX or is the whole concept wrong?



For simplicity of testing, I did not add any dynamic controls but did place a text box and a button on the form. I placed the above code in the unload event.
Thank you.
 

cachua9

Member
Joined
Feb 25, 2005
Messages
11
Programming Experience
Beginner
I have the same problem, but it says "Textbox is not member of System.UI.WebControls....."

I don't know what you try to do through your textboxes.
In my ap, I want to validate user input such as requirement fields, phone # and email addresses are in right format. So that, I used validators controls.
It works well and easy.
You can try
 

panna_sn

Member
Joined
Apr 28, 2009
Messages
11
Programming Experience
1-3
Better to place the controls in a table. Then

Foreach ctl in Table1.Controls
If ctl.GetType().ToString().Equals("System.Web.UI.Web Controls.TextBox") Then

‘Do Something

End If

Next

Thanks
Panna
 

billsut

Member
Joined
May 29, 2009
Messages
6
Programming Experience
10+
I use a placeholder control.


Dim FieldTypes As New FieldTypes
Dim divContainer As HtmlGenericControl = New HtmlGenericControl("div")
Dim strQuestionInfo As String = ""
Dim strQuestion As String = ""

Dim strSQl As String = ""

strSQl = "select title from tbl_surveys where surveyid = '" & SurveyID.Value & "'"

openConnection()

Dim cmdTitle As New SqlCommand(strSQl, objConnection)
Dim drTitle As SqlDataReader = cmdTitle.ExecuteReader
While drTitle.Read
strTitle = drTitle("title")
End While

closeConnection()

strQuestionInfo = "Select questionid, fieldtype, question " & _
"from tbl_questions " & _
"where surveyid = '" & SurveyID.Value & "'"

openConnection()

Dim cmdQuestion As New SqlCommand(strQuestionInfo, objConnection)
Dim drQuestion As SqlDataReader = cmdQuestion.ExecuteReader
While drQuestion.Read

FieldTypes.p_QuestionID = drQuestion("QuestionID")
strQuestion = drQuestion("question")

Select Case drQuestion("fieldtype")
'1 - Textbox
'2 - Select List
'3 - Checkbox
'4 - Radio
'5 - Text Area
'7 - Instruction ??? Add document or url here???
'8 - Image
'9 - Licart Scale
'10 - Horizontal Rule
'11 - Line break

Case 1

divContainer.Controls.Add(new LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl(strQuestion))
divContainer.Controls.Add(New LiteralControl("<br/>"))
Dim box As New TextBox
divContainer.Controls.Add(box)
divContainer.Controls.Add(new LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl("<br/>"))

Case 2

FieldTypes.m_DropDownList

divContainer.Controls.Add(new LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl(strQuestion))
divContainer.Controls.Add(New LiteralControl("<br/>"))
divContainer.Controls.Add(FieldTypes.p_drpAnswers)
divContainer.Controls.Add(new LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl("<br/>"))

Case 3

FieldTypes.m_CheckBoxList

divContainer.Controls.Add(new LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl(strQuestion))
divContainer.Controls.Add(New LiteralControl("<br/>"))
divContainer.Controls.Add(FieldTypes.p_chkAnswers)
divContainer.Controls.Add(new LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl("<br/>"))

Case 4

FieldTypes.m_RadioButtonList

divContainer.Controls.Add(new LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl(strQuestion))
divContainer.Controls.Add(New LiteralControl("<br/>"))
divContainer.Controls.Add(FieldTypes.p_rdoAnswers)
divContainer.Controls.Add(new LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl("<br/>"))

Case 5

divContainer.Controls.Add(new LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl(strQuestion))
divContainer.Controls.Add(New LiteralControl("<br/>"))
Dim box As New Textbox
box.Columns = 100
box.Rows = 6
box.TextMode = TextBoxMode.MultiLine
divContainer.Controls.Add(box)
divContainer.Controls.Add(new LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl("<br/>"))

Case 7

divContainer.Controls.Add(New LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl(strQuestion))
divContainer.Controls.Add(New LiteralControl("<br/>"))
divContainer.Controls.Add(New LiteralControl("<br/>"))

Case 8

divContainer.Controls.Add(New LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl(strQuestion))
divContainer.Controls.Add(New LiteralControl("<br/>"))
divContainer.Controls.Add(New LiteralControl("<br/>"))

Case 9

FieldTypes.m_rdoLicartScale

divContainer.Controls.Add(new LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl(strQuestion))
'divContainer.Controls.Add(New LiteralControl("<br/>"))
divContainer.Controls.Add(FieldTypes.p_lcsAnswers)
divContainer.Controls.Add(new LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl("<br/>"))

Case 10

divContainer.Controls.Add(New LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl("<hr width='100%' size='1' style='BORDER-RIGHT: solid; BORDER-TOP: solid; BORDER-LEFT: solid; COLOR: #666666; BORDER-BOTTOM: solid'>"))
divContainer.Controls.Add(New LiteralControl("<br/>"))
divContainer.Controls.Add(New LiteralControl("<br/>"))



End Select

End While

closeConnection()

SurveyItems.Controls.Add(divContainer)

<tr>
<td>
<div id="SurveyItems" runat="server"></div>
</td>
</tr>
 
Joined
Dec 21, 2009
Messages
24
Location
India
Programming Experience
1-3
Want to know the database Part

hi
i want to know the tables and fields so i that i want to see the working of dynamic controls since i have a need for it .
 
Top Bottom