Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Reference value of dynamic control in VB ASP.NET

Posted on 2011-09-14
5
Medium Priority
?
276 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
[X]
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
  • 2
  • 2
5 Comments
 
LVL 16

Accepted Solution

by:
disrupt earned 1000 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 1000 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

604 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