473,320 Members | 2,071 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

catch values of dynamic controls

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
3 3079
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: tascien | last post by:
I have a user control that has dynamic controls loaded to it in Page_Init. this works well. I have a function inside the user control: GetValues() When i add this usercontrol to a page:...
3
by: SD | last post by:
Hello, I have a form that has a panel container where I'm adding textboxes dynamically based on a query to database, so I'm looping through the records. The problem I have is that once built,...
4
by: Larry Grady | last post by:
Anyone up for a challenge? I've been struggling with this for a few days and was hoping someone could help me. Pouring through all the messageboards I just can't find the solution. We have a...
1
by: Joe | last post by:
Hello All, I have a user control which is composed of a label and a dropdownlist. In my code I add the user control to a placeholder on the webform. Now I want to be able to retrieve the...
2
by: ayende | last post by:
I've a very strange issue here. I've this JS code: var hidden = document.createElement('input') //hidden.type='hidden'; hidden.value = getIdForUser(userTag)...
5
by: Nathan Sokalski | last post by:
I have a custom control that I wrote (I inherit from System.Web.UI.WebControls.CompositeControl). I dynamically add this control to my Page, but I was told that dynamically added controls do not...
5
by: vovan | last post by:
I have set of controls (Textboxes, checkboxes etc) along with the Grid on Windows Form. I use BindingSource to populate both Grid and the set of Controls. User selects the record in the grid and...
2
by: =?Utf-8?B?TG91aXNhOTk=?= | last post by:
Hello. ive just read some posts on the age old issue of losing dynamic control values on postback. Most people say recreate the controls on the onInit, but i just dont think this serves my purpose....
2
by: bharathi228 | last post by:
my code for retrieving values from database Dim da As New SqlDataAdapter("select parameter_name,parameter_units from sys_params", con) If con.State = ConnectionState.Closed Then con.Open()...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.