By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,636 Members | 1,693 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,636 IT Pros & Developers. It's quick & easy.

catch values of dynamic controls

P: n/a
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
Mar 23 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" wrote:
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
Mar 23 '07 #2

P: n/a
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 [MCAD]" <mi*****@DONTLIKESPAMwp.plschrieb im Newsbeitrag
news:12**********************************@microsof t.com...
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" wrote:
>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

Mar 24 '07 #3

P: n/a
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" wrote:
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 [MCAD]" <mi*****@DONTLIKESPAMwp.plschrieb im Newsbeitrag
news:12**********************************@microsof t.com...
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" wrote:
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


Mar 28 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.