Scrolling through my controls on a page

S

Scott

I created a custom web control and placed a couple instances on an aspx
page. The idea is that the user needs to make a selection in each control
and then click on a button to save the answers in each control to a
database. How can I programmatically scroll through all the controls on the
page to retrieve the values in each custom control? Here is my best shot
which doesn't work, but should give you an idea of what I am trying to do:

Private Sub Button1_Click(ByVal sender ....) Handles Button1.Click
Dim item As Control
For each item in Me.Controls
'Code to check each control to see if we need to save its value.
Response.Write(item.ID.ToString & "<br>")
Next
End Sub.

I need to pick up on the control's ID or type... any suggestions on how I
can do that?

Scott
 
A

Alessandro Zifiglio

hi Scott,
You need to check against a specific control and then cast it to that
control before you can retrieve its ID or any other property. Same way I'm
retrieving the ID
you can retrieve other properties or your custom control.

Private Sub Button1_Click(ByVal sender ....) Handles Button1.Click
Dim MyControlID as String
Dim Item As Control
For Each Item in Me.Controls
If TypeOf Item Is MyCustomControl Then
MyControlID = CType(Item, MyCustomControl).ID
End If
Next
End Sub
 
J

Jeffrey Tan[MSFT]

Hi Scott,

Thanks for posting in this group.
Actully, when you add control on the web page, it will be added into the
form's control collection, so you should loop through the Form's Controls
property.
To get the programming interface to form element, you should add a HtmlForm
declearation for this form. Do something like this:

Protected Form1 As System.Web.UI.HtmlControls.HtmlForm
Private Sub Button1_Click(ByVal sender ....) Handles Button1.Click
Dim item As Control
For each item in Form1.Controls
if item is TextBox or item is Button then
Response.Write(item.ID.ToString & "<br>")
end if
Next
End Sub.

Any HTML tags or text strings that are not processed on the server are
treated as LiteralControl objects. These are added to the collection like
other server controls. They have no ID properties(null). So you should
check the control's type(In the code snippet, I only check TextBox and
Button)

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
S

Scott

I successfully modified your code to scroll through the objects on the form,
but I need to go deeper to get what I need. In my program I created a
custom control called InputWebControls which has a class in one of the vb
files called "MATextbox". The job of the class is to go through a SQL
table, retrieve the questions for the form and then display them as text and
empty text boxes for the user to enter an answer into. The MATextbox class
uses the "Protected Overrides Sub Render()" procedure to send a string back
to the form which defines the text boxes (which will hold the answers) as
"<input type="text" ...>" When the page renders its source shows a <form>
section with text and <input> items. This all works great.

When the user clicks the submit button I wrote your code into the Page_Load
on isPostBack as follows:

For Each item in form1.Controls
If TypeOf item is InputWebControls.MATextbox Then
Response.write(item.ID.ToSTring & "<br>")
End If
Next

This code does return a string "MATextbox2", which is the name of the
control on the form, but what I need is to scroll through the input boxes in
that custom control and return their ID values (the GUIDs that are related
to the SQL table's ID column) and the contents of the text boxes. Is there
a way I can do this? If this is not possible, is there another technique I
can use to do what is needed?

Scott

"Jeffrey Tan[MSFT]" said:
Hi Scott,

Thanks for posting in this group.
Actully, when you add control on the web page, it will be added into the
form's control collection, so you should loop through the Form's Controls
property.
To get the programming interface to form element, you should add a HtmlForm
declearation for this form. Do something like this:

Protected Form1 As System.Web.UI.HtmlControls.HtmlForm
Private Sub Button1_Click(ByVal sender ....) Handles Button1.Click
Dim item As Control
For each item in Form1.Controls
if item is TextBox or item is Button then
Response.Write(item.ID.ToString & "<br>")
end if
Next
End Sub.

Any HTML tags or text strings that are not processed on the server are
treated as LiteralControl objects. These are added to the collection like
other server controls. They have no ID properties(null). So you should
check the control's type(In the code snippet, I only check TextBox and
Button)

Hope this helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi Scott,

Thanks for your feedback.
Based on my understanding, your custom control renders serveral html
textboxes, so you want to get the collection of these textboxes.
First, I want to remind you that your custom control is a server side
control(class), the textboxes you render out is client side html code,
which have no object life time in server side. Also, they are not the child
controls of your custom control.
Second, if you want to use client side html DOM to get the textbox
collection, you can not seperate your rendered textbox and other way
generated textbox.
So I think you should get this done at server side.
Because the custom control is created by yourself, I think you can
determine all the referred content at the end of the render method.
I think you can provide a public method of your custom control which
returns all the renderred textbox information. Then in the form, you can
invoke this method to get the information.

Hope it helps,

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

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

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top