473,607 Members | 2,674 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

catch values of dynamic controls

Hi Group!

I have a web form that is created dynamically - so I create Textboxes,
RadioButtonList s, 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 mpContentPlaceH older As ContentPlaceHol der
mpContentPlaceH older = CType(Master.Fi ndControl("cont ent"),
ContentPlaceHol der)
Dim mp As MultiPage
mp = CType(mpContent PlaceHolder.Fin dControl("mp"), MultiPage)
'Debug.Print("v ieSTateval " & Me.ViewState("T extBox1").ToStr ing)
For Each ctl In mp.Controls ' Pages!
If TypeOf ctl Is PageView Then
If ctl.Controls.Co unt 0 Then
table = ctl.Controls(0)
For Each row In table.Controls
If row.Controls.Co unt 1 Then
cell = row.Controls(1)
dataControl = cell.Controls(0 )

' if i make an output of the ID all controls are
iterated!!!
Debug.Print("va lue of " & dataControl.ID & " is "
& getDataCellValu e(dataControl)
End If
Next
End If
End If
Next
For Each ctl In Me.Form.Control s
'Debug.Print("N ame " & ctl.ID & " value " & ctl.ToString)
Next
End Sub

Private Function getDataCellValu e(ByRef ctl As Control) As String
Dim t As TextBox
If TypeOf ctl Is TextBox Then
t = CType(ctl, TextBox)
getDataCellValu e = 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 3092
Howdy,

Private Function getDataCellValu e(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).Se lectedValue
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,
RadioButtonList s, 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 mpContentPlaceH older As ContentPlaceHol der
mpContentPlaceH older = CType(Master.Fi ndControl("cont ent"),
ContentPlaceHol der)
Dim mp As MultiPage
mp = CType(mpContent PlaceHolder.Fin dControl("mp"), MultiPage)
'Debug.Print("v ieSTateval " & Me.ViewState("T extBox1").ToStr ing)
For Each ctl In mp.Controls ' Pages!
If TypeOf ctl Is PageView Then
If ctl.Controls.Co unt 0 Then
table = ctl.Controls(0)
For Each row In table.Controls
If row.Controls.Co unt 1 Then
cell = row.Controls(1)
dataControl = cell.Controls(0 )

' if i make an output of the ID all controls are
iterated!!!
Debug.Print("va lue of " & dataControl.ID & " is "
& getDataCellValu e(dataControl)
End If
Next
End If
End If
Next
For Each ctl In Me.Form.Control s
'Debug.Print("N ame " & ctl.ID & " value " & ctl.ToString)
Next
End Sub

Private Function getDataCellValu e(ByRef ctl As Control) As String
Dim t As TextBox
If TypeOf ctl Is TextBox Then
t = CType(ctl, TextBox)
getDataCellValu e = 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("91 2")

If (Not myCtl Is Nothing) Then

Response.Write( "controlval ue is : " & CType(myCtl, TextBox).Text.T oString)

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*****@DONTLI KESPAMwp.plschr ieb im Newsbeitrag
news:12******** *************** ***********@mic rosoft.com...
Howdy,

Private Function getDataCellValu e(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).Se lectedValue
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,
RadioButtonLis ts, 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 mpContentPlaceH older As ContentPlaceHol der
mpContentPlaceH older = CType(Master.Fi ndControl("cont ent"),
ContentPlaceHo lder)
Dim mp As MultiPage
mp = CType(mpContent PlaceHolder.Fin dControl("mp"), MultiPage)
'Debug.Print("v ieSTateval " & Me.ViewState("T extBox1").ToStr ing)
For Each ctl In mp.Controls ' Pages!
If TypeOf ctl Is PageView Then
If ctl.Controls.Co unt 0 Then
table = ctl.Controls(0)
For Each row In table.Controls
If row.Controls.Co unt 1 Then
cell = row.Controls(1)
dataControl = cell.Controls(0 )

' if i make an output of the ID all controls
are
iterated!!!
Debug.Print("va lue of " & dataControl.ID & "
is "
& getDataCellValu e(dataControl)
End If
Next
End If
End If
Next
For Each ctl In Me.Form.Control s
'Debug.Print("N ame " & ctl.ID & " value " & ctl.ToString)
Next
End Sub

Private Function getDataCellValu e(ByRef ctl As Control) As String
Dim t As TextBox
If TypeOf ctl Is TextBox Then
t = CType(ctl, TextBox)
getDataCellValu e = 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("91 2")

If (Not myCtl Is Nothing) Then

Response.Write( "controlval ue is : " & CType(myCtl, TextBox).Text.T oString)

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*****@DONTLI KESPAMwp.plschr ieb im Newsbeitrag
news:12******** *************** ***********@mic rosoft.com...
Howdy,

Private Function getDataCellValu e(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).Se lectedValue
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,
RadioButtonList s, 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 mpContentPlaceH older As ContentPlaceHol der
mpContentPlaceH older = CType(Master.Fi ndControl("cont ent"),
ContentPlaceHol der)
Dim mp As MultiPage
mp = CType(mpContent PlaceHolder.Fin dControl("mp"), MultiPage)
'Debug.Print("v ieSTateval " & Me.ViewState("T extBox1").ToStr ing)
For Each ctl In mp.Controls ' Pages!
If TypeOf ctl Is PageView Then
If ctl.Controls.Co unt 0 Then
table = ctl.Controls(0)
For Each row In table.Controls
If row.Controls.Co unt 1 Then
cell = row.Controls(1)
dataControl = cell.Controls(0 )

' if i make an output of the ID all controls
are
iterated!!!
Debug.Print("va lue of " & dataControl.ID & "
is "
& getDataCellValu e(dataControl)
End If
Next
End If
End If
Next
For Each ctl In Me.Form.Control s
'Debug.Print("N ame " & ctl.ID & " value " & ctl.ToString)
Next
End Sub

Private Function getDataCellValu e(ByRef ctl As Control) As String
Dim t As TextBox
If TypeOf ctl Is TextBox Then
t = CType(ctl, TextBox)
getDataCellValu e = 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
1271
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: test.aspx and i call GetValues(), it returns me the values of the dynamically added controls correctly.
3
1234
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, I need to pass those textboxes values to another for to build the parameters for a report. Here is the sample:
4
5030
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 GridView that needs to be dynamically designed, depending on what collection of fields our uses want to edit for their product data. We have 400+ fields of information per product so they're selecting a subset of those fields to edit.
1
2912
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 selected value from the drop downlist when a user clicks a button server control. Here is the code that populates the user control and placeholder: Dim UControl As AugmentedDropDownList =
2
1351
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) $('assignedUsersInputs').appendChild(hidden); var i =0; for(var node = $('assignedUsersInputs').firstChild;node !=null; node = node.nextSibling) {
5
4447
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 survive postback. This seems to be true. However, this seems to make dynamically adding controls rather pointless to me, since if you cannot retrieve the values of the controls when you perform a postback, why add the controls in the firstplace?...
5
5174
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 all controls are populated with data from the selected row. The grid is going to be read only. Textboxes, checkboxes are going to be read/write. What event and how do I need to use to catch any change in any of the textboxes? I tried to use...
2
4244
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. if i could explain: i have a list of reports in a datalist. a user clicks a report link. it goes off to reporting services, fetches the parameters for this report and displays x number of textboxes (and labels) for those parameters (hence it...
2
1385
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() End If Dim ds As New DataSet Dim name, unit ,namunt As String da.Fill(ds, "sys_params")
0
8469
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8463
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6803
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5997
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4013
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2461
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1574
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.