how do I loop through htmlcontols in the code behind page..

H

Hazzard

How would I loop through in a code behind page event the following two
controls given the code behind and html below? (c# or vb.net)
I would like to access the name of the control, the id, and the value?
thx...
*************************************************************************
Protected WithEvents txtLastName As System.Web.UI.HtmlControls.HtmlInputText
Protected WithEvents txtPhone As System.Web.UI.HtmlControls.HtmlInputText
**********************************************************
< input id=txtLastName type=text maxLength=40 size=25 name=lname
runat="server">
<input id=txtContactTitle type=text maxLength=30 size=25
name=title runat="server">
*********************
Thank you, Greg
 
K

Ken Cox [Microsoft MVP]

Hi Greg,

You need to get the Controls collection from the Form (Form1) and loop
through each one to see if it is an HtmlInputText control. If so, you can
dig out its ID, value and name properties. Here's a quick sample:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim frmCntrl As Control
Dim cntrl As Control
Dim txtbox As _
System.Web.UI.HtmlControls.HtmlInputText
frmCntrl = Page.FindControl("Form1")
For Each cntrl _
In frmCntrl.Controls
If TypeOf cntrl Is _
System.Web.UI.HtmlControls.HtmlInputText Then
txtbox = cntrl
Response.Write _
(txtbox.ID & ":" & txtbox.Value & ":" & _
txtbox.Name & "<br>")
End If
Next
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]
Toronto
 
H

Hazzard

Thank you Ken!

I was completely ignorant about the Page.Findcontrol method. That is what I
was missing.

And how did you know my formID was 'Form1'? ;-)

Thank you so much for your quick and correct response.

You really helped me to gain a huge jump on my weekly goals !

Bless your heart.
Greg


Ken Cox said:
Hi Greg,

You need to get the Controls collection from the Form (Form1) and loop
through each one to see if it is an HtmlInputText control. If so, you can
dig out its ID, value and name properties. Here's a quick sample:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim frmCntrl As Control
Dim cntrl As Control
Dim txtbox As _
System.Web.UI.HtmlControls.HtmlInputText
frmCntrl = Page.FindControl("Form1")
For Each cntrl _
In frmCntrl.Controls
If TypeOf cntrl Is _
System.Web.UI.HtmlControls.HtmlInputText Then
txtbox = cntrl
Response.Write _
(txtbox.ID & ":" & txtbox.Value & ":" & _
txtbox.Name & "<br>")
End If
Next
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]
Toronto

Hazzard said:
How would I loop through in a code behind page event the following two
controls given the code behind and html below? (c# or vb.net)
I would like to access the name of the control, the id, and the value?
thx...
*************************************************************************
Protected WithEvents txtLastName As
System.Web.UI.HtmlControls.HtmlInputText
Protected WithEvents txtPhone As System.Web.UI.HtmlControls.HtmlInputText
**********************************************************
< input id=txtLastName type=text maxLength=40 size=25 name=lname
runat="server">
<input id=txtContactTitle type=text maxLength=30 size=25
name=title runat="server">
*********************
Thank you, Greg
 
K

Ken Cox [Microsoft MVP]

Hey Greg,

Glad to be of service!

Drop by anytime. We have a good community here.

Ken
 
J

Jeffrey Tan[MSFT]

Hi Greg,

Based on my understanding, you have some html controls in your aspx file,
you want to refer them in code behind file.

Normally, Html control will not add the reference in code behind file, in
order to refer it and use it, you have to add the reference yourself.

In the code behind, you may add 2 references with the reference name the
same as the id attribute of the html control. Like this:
< input id=txtLastName type=text maxLength=40 size=25 name=lname
runat="server">
<input id=txtContactTitle type=text maxLength=30 size=25
name=title runat="server">

Protected WithEvents txtLastName As System.Web.UI.HtmlControls.HtmlInputText
Protected WithEvents txtContactTitle As
System.Web.UI.HtmlControls.HtmlInputText

Then, VS.net will automatically associate your manually added references
with this 2 html controls.

The same reason, we normally refer the <form> html control through:
Protected Form1 As System.Web.UI.HtmlControlsHtmlForm

Note: in code behind, you should specify the assessor for "protected" or
"public", you can not specify it as "private"

============================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

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 Greg,

Ken gives you a dynamical way of getting the HtmlForm html control at
run-time. As an alternative, you can use my way of referring HtmlForm. Just
add
Protected Form1 As System.Web.UI.HtmlControlsHtmlForm in the code behind,
the VS.net will automatically associate this reference with your HtmlForm.

For your further question of:
how did you know my formID was 'Form1'?

This is because, VS.net default generated 'Form1' as the html form
control's id.

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.
 
K

Ken Cox [Microsoft MVP]

Hey Jeffrey,

He was joking about "Form1". You missed the " ;-)"

Ken
 

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

Top