catch values of dynamic controls

A

Andreas Wöckl

Hi Group!

I have a web form that is created dynamically - so I create Textboxes,
RadioButtonLists, CheckBoxLists and so on - I have found some articles that
there may be some problems with postback - but if I press a submit button
the textboxes keep the value. My problem is the following:

I would like to use this Code to collect the data:

'Collects values - is calles from a button

Private Sub collectValues()
Dim page As Control
Dim ctl As Control
Dim table As Table
Dim row As Control
Dim cell As TableCell
Dim dataControl As Control
Dim mpContentPlaceHolder As ContentPlaceHolder
mpContentPlaceHolder = CType(Master.FindControl("content"),
ContentPlaceHolder)
Dim mp As MultiPage
mp = CType(mpContentPlaceHolder.FindControl("mp"), MultiPage)
'Debug.Print("vieSTateval " & Me.ViewState("TextBox1").ToString)
For Each ctl In mp.Controls ' Pages!
If TypeOf ctl Is PageView Then
If ctl.Controls.Count > 0 Then
table = ctl.Controls(0)
For Each row In table.Controls
If row.Controls.Count > 1 Then
cell = row.Controls(1)
dataControl = cell.Controls(0)

' if i make an output of the ID all controls are
iterated!!!
Debug.Print("value of " & dataControl.ID & " is "
& getDataCellValue(dataControl)
End If
Next
End If
End If
Next
For Each ctl In Me.Form.Controls
'Debug.Print("Name " & ctl.ID & " value " & ctl.ToString)
Next
End Sub

Private Function getDataCellValue(ByRef ctl As Control) As String
Dim t As TextBox
If TypeOf ctl Is TextBox Then
t = CType(ctl, TextBox)
getDataCellValue = t.ToString
ElseIf TypeOf ctl Is RadioButtonList Then

ElseIf TypeOf ctl Is CheckBoxList Then

End If
End Function

I always get the following output: "value of [id] is " - So when iterating
the controls I can not access the values - but I still can see them in the
form after the postback!

Anyone an idea?

best regards

Andy
 
G

Guest

Howdy,

Private Function getDataCellValue(ByVal ctl As Control) As String
If TypeOf ctl Is TextBox Then
return CType(ctl, TextBox).Text
ElseIf TypeOf ctl Is ListControl Then
return CType(ctl, ListControl).SelectedValue
Else
return String.Empty
End If
End Function

Two things:
1. Put a breakpoint to debug and make sure you're passing correct controls
(in the line dataControl = cell.Controls(0))
2. You dont's need to pass control by reference but by value. Most of the vb
programmers have difficulty to understand that using byref in conjunction
with object they passing reference to reference. passing object by val copies
reference (4bytes unsignet int) to the function, but object stays in the same
place where it was before and you access the same instance from inside the
function - in other words object is not copied, the reference (address) is
copied and passed to the function.

Hope this helps
--
Milosz


Andreas Wöckl said:
Hi Group!

I have a web form that is created dynamically - so I create Textboxes,
RadioButtonLists, CheckBoxLists and so on - I have found some articles that
there may be some problems with postback - but if I press a submit button
the textboxes keep the value. My problem is the following:

I would like to use this Code to collect the data:

'Collects values - is calles from a button

Private Sub collectValues()
Dim page As Control
Dim ctl As Control
Dim table As Table
Dim row As Control
Dim cell As TableCell
Dim dataControl As Control
Dim mpContentPlaceHolder As ContentPlaceHolder
mpContentPlaceHolder = CType(Master.FindControl("content"),
ContentPlaceHolder)
Dim mp As MultiPage
mp = CType(mpContentPlaceHolder.FindControl("mp"), MultiPage)
'Debug.Print("vieSTateval " & Me.ViewState("TextBox1").ToString)
For Each ctl In mp.Controls ' Pages!
If TypeOf ctl Is PageView Then
If ctl.Controls.Count > 0 Then
table = ctl.Controls(0)
For Each row In table.Controls
If row.Controls.Count > 1 Then
cell = row.Controls(1)
dataControl = cell.Controls(0)

' if i make an output of the ID all controls are
iterated!!!
Debug.Print("value of " & dataControl.ID & " is "
& getDataCellValue(dataControl)
End If
Next
End If
End If
Next
For Each ctl In Me.Form.Controls
'Debug.Print("Name " & ctl.ID & " value " & ctl.ToString)
Next
End Sub

Private Function getDataCellValue(ByRef ctl As Control) As String
Dim t As TextBox
If TypeOf ctl Is TextBox Then
t = CType(ctl, TextBox)
getDataCellValue = t.ToString
ElseIf TypeOf ctl Is RadioButtonList Then

ElseIf TypeOf ctl Is CheckBoxList Then

End If
End Function

I always get the following output: "value of [id] is " - So when iterating
the controls I can not access the values - but I still can see them in the
form after the postback!

Anyone an idea?

best regards

Andy
 
A

Andreas Wöckl

Hi Milosz!

Thanks for your answer - it's working now!!

Just another question concerning dynamic controls:

I use this code (control with ID 912 exists!
myCtl = FindControl("912")

If (Not myCtl Is Nothing) Then

Response.Write("controlvalue is : " & CType(myCtl, TextBox).Text.ToString)

Else

Response.Write("Control not found.....")

End If

But I can not access the control via findControl? - Of course I have a
control hierarchy with TabStrip, PageView, Table and so on - I thought
findControl works without worrying about the hierarchy?

best regards

andy


Milosz Skalecki said:
Howdy,

Private Function getDataCellValue(ByVal ctl As Control) As String
If TypeOf ctl Is TextBox Then
return CType(ctl, TextBox).Text
ElseIf TypeOf ctl Is ListControl Then
return CType(ctl, ListControl).SelectedValue
Else
return String.Empty
End If
End Function

Two things:
1. Put a breakpoint to debug and make sure you're passing correct controls
(in the line dataControl = cell.Controls(0))
2. You dont's need to pass control by reference but by value. Most of the
vb
programmers have difficulty to understand that using byref in conjunction
with object they passing reference to reference. passing object by val
copies
reference (4bytes unsignet int) to the function, but object stays in the
same
place where it was before and you access the same instance from inside the
function - in other words object is not copied, the reference (address) is
copied and passed to the function.

Hope this helps
--
Milosz


Andreas Wöckl said:
Hi Group!

I have a web form that is created dynamically - so I create Textboxes,
RadioButtonLists, CheckBoxLists and so on - I have found some articles
that
there may be some problems with postback - but if I press a submit button
the textboxes keep the value. My problem is the following:

I would like to use this Code to collect the data:

'Collects values - is calles from a button

Private Sub collectValues()
Dim page As Control
Dim ctl As Control
Dim table As Table
Dim row As Control
Dim cell As TableCell
Dim dataControl As Control
Dim mpContentPlaceHolder As ContentPlaceHolder
mpContentPlaceHolder = CType(Master.FindControl("content"),
ContentPlaceHolder)
Dim mp As MultiPage
mp = CType(mpContentPlaceHolder.FindControl("mp"), MultiPage)
'Debug.Print("vieSTateval " & Me.ViewState("TextBox1").ToString)
For Each ctl In mp.Controls ' Pages!
If TypeOf ctl Is PageView Then
If ctl.Controls.Count > 0 Then
table = ctl.Controls(0)
For Each row In table.Controls
If row.Controls.Count > 1 Then
cell = row.Controls(1)
dataControl = cell.Controls(0)

' if i make an output of the ID all controls
are
iterated!!!
Debug.Print("value of " & dataControl.ID & "
is "
& getDataCellValue(dataControl)
End If
Next
End If
End If
Next
For Each ctl In Me.Form.Controls
'Debug.Print("Name " & ctl.ID & " value " & ctl.ToString)
Next
End Sub

Private Function getDataCellValue(ByRef ctl As Control) As String
Dim t As TextBox
If TypeOf ctl Is TextBox Then
t = CType(ctl, TextBox)
getDataCellValue = t.ToString
ElseIf TypeOf ctl Is RadioButtonList Then

ElseIf TypeOf ctl Is CheckBoxList Then

End If
End Function

I always get the following output: "value of [id] is " - So when
iterating
the controls I can not access the values - but I still can see them in
the
form after the postback!

Anyone an idea?

best regards

Andy
 
G

Guest

Hi Andreas,

Due to performance reasons FindControl does not search recursively (meaning
nested controls are not included in the search). However, you can specify
possible location using $ separator (to be precise, every server control
derives protected readonly property called IdSeparator which holds the value
of the id separator used to build ID that includes all the parents ids). This
can only be applied if you know the most nested control that contains the
control you're looking for. Otherwise, you must call FindControl recursively
for each control in Controls collection.

Hope this helps
--
Milosz

Andreas Wöckl said:
Hi Milosz!

Thanks for your answer - it's working now!!

Just another question concerning dynamic controls:

I use this code (control with ID 912 exists!
myCtl = FindControl("912")

If (Not myCtl Is Nothing) Then

Response.Write("controlvalue is : " & CType(myCtl, TextBox).Text.ToString)

Else

Response.Write("Control not found.....")

End If

But I can not access the control via findControl? - Of course I have a
control hierarchy with TabStrip, PageView, Table and so on - I thought
findControl works without worrying about the hierarchy?

best regards

andy


Milosz Skalecki said:
Howdy,

Private Function getDataCellValue(ByVal ctl As Control) As String
If TypeOf ctl Is TextBox Then
return CType(ctl, TextBox).Text
ElseIf TypeOf ctl Is ListControl Then
return CType(ctl, ListControl).SelectedValue
Else
return String.Empty
End If
End Function

Two things:
1. Put a breakpoint to debug and make sure you're passing correct controls
(in the line dataControl = cell.Controls(0))
2. You dont's need to pass control by reference but by value. Most of the
vb
programmers have difficulty to understand that using byref in conjunction
with object they passing reference to reference. passing object by val
copies
reference (4bytes unsignet int) to the function, but object stays in the
same
place where it was before and you access the same instance from inside the
function - in other words object is not copied, the reference (address) is
copied and passed to the function.

Hope this helps
--
Milosz


Andreas Wöckl said:
Hi Group!

I have a web form that is created dynamically - so I create Textboxes,
RadioButtonLists, CheckBoxLists and so on - I have found some articles
that
there may be some problems with postback - but if I press a submit button
the textboxes keep the value. My problem is the following:

I would like to use this Code to collect the data:

'Collects values - is calles from a button

Private Sub collectValues()
Dim page As Control
Dim ctl As Control
Dim table As Table
Dim row As Control
Dim cell As TableCell
Dim dataControl As Control
Dim mpContentPlaceHolder As ContentPlaceHolder
mpContentPlaceHolder = CType(Master.FindControl("content"),
ContentPlaceHolder)
Dim mp As MultiPage
mp = CType(mpContentPlaceHolder.FindControl("mp"), MultiPage)
'Debug.Print("vieSTateval " & Me.ViewState("TextBox1").ToString)
For Each ctl In mp.Controls ' Pages!
If TypeOf ctl Is PageView Then
If ctl.Controls.Count > 0 Then
table = ctl.Controls(0)
For Each row In table.Controls
If row.Controls.Count > 1 Then
cell = row.Controls(1)
dataControl = cell.Controls(0)

' if i make an output of the ID all controls
are
iterated!!!
Debug.Print("value of " & dataControl.ID & "
is "
& getDataCellValue(dataControl)
End If
Next
End If
End If
Next
For Each ctl In Me.Form.Controls
'Debug.Print("Name " & ctl.ID & " value " & ctl.ToString)
Next
End Sub

Private Function getDataCellValue(ByRef ctl As Control) As String
Dim t As TextBox
If TypeOf ctl Is TextBox Then
t = CType(ctl, TextBox)
getDataCellValue = t.ToString
ElseIf TypeOf ctl Is RadioButtonList Then

ElseIf TypeOf ctl Is CheckBoxList Then

End If
End Function

I always get the following output: "value of [id] is " - So when
iterating
the controls I can not access the values - but I still can see them in
the
form after the postback!

Anyone an idea?

best regards

Andy
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top