Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Reference value of dynamic control in VB ASP.NET

Posted on 2011-09-14
5
266 Views
Last Modified: 2012-05-12
I have an ASP.NET web page that dynamically creates a table with 3 columns and a variable number of rows.

Col 1 contains a checkbox - chkx
Col 2 contains a textbox - txtx
Col 3 contains a label - lblx

where x is a number based on how many times we've ran round the loop.

All the above are created dynamically during the page init event and are givin an ID at run-time. (code snipit below)

Everything works fine, however, when I click one of the checkboxes I need to get the value in the coresponding textbox & label

i.e. when I click chk1, then I need the value in txt1 & lbl1, but as the controls are dynamic, the code behind doesn't recognise the ID of the control, so... txt1.Text fails validation..

So how do I reference the control's value
Dim nCheckBox As CheckBox = New CheckBox()
nCheckBox.Checked = False
nCheckBox.EnableViewState = True
nCheckBox.ID = "chk" & counter
nCheckBox.AutoPostBack = True

AddHandler nCheckBox.CheckedChanged, AddressOf CheckBox1_CheckedChanged

Open in new window

0
Comment
Question by:BrianFord
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
disrupt earned 250 total points
ID: 36540100
Take a look at this:

http://techbrij.com/197/retrieve-value-of-dynamic-controls-in-asp-net

shows you how to retrieve dynamic controls
0
 

Author Comment

by:BrianFord
ID: 36540158
thanks,

but I find that hard to follow as it's C# and I'm not familiar with that, I'm using VB

It looks like I have to iterate through all the dynamic control to see if it's a checkbox, then if it's a checkbox and it's checked I still don't see how to get the value from the textbox that's in the adjacent cell of the same row?
0
 
LVL 16

Expert Comment

by:disrupt
ID: 36540247
you can use this to conver to vb .net:

http://www.developerfusion.com/tools/convert/csharp-to-vb/
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
ID: 36570554
Try using the FindControl method of the container. The container would be the page if the controls are directly added to page or a form or a panel or a grid etc. So you can use

CType(Page.FindControl("txt1"), TextBox).Text
0
 

Author Closing Comment

by:BrianFord
ID: 36570640
thank you very much
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question