Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access TextBox Dynamically 2

Status
Not open for further replies.

Steve101

Programmer
Mar 29, 2002
1,473
AU
am sure that the answer to this is quite simple, but I am just not seeing it:

I have a number of text controls on a web page; for example:
<asp:TextBox ID="ETAC1" runat="server"></asp:TextBox>
<asp:TextBox ID="ETAC2" runat="server"></asp:TextBox>
<asp:TextBox ID="ETAC3" runat="server"></asp:TextBox>
<asp:TextBox ID="ETAC4" runat="server"></asp:TextBox>
Whilst I can happily reference these individually; for example:
ETAC1.Text = 10
ETAC2.Text = 20
ETAC3.Text = 30
ETAC4.Text = 40
I have not been able to figure how I can reference these controls programmatically. For example, I would like to do something like:
For i = 1 to 4
thisControlId = "ETAC" & i
Controls(ThisControlId).Text = i*10 ' <-----------
Next

I am using a master page and Contentholder to construct my page.
I have been advised to try:
cn="ETAC1"
ctype(me.findcontrol(cn),textbox).text=10*i
This however resulted in the error:
"NullReferenceException was unhandled by user code"
"Object Reference not set to an instance of an object"

It doesnt seem to be finding the Control, even though the static reference to it resolves without problem.

I believe that I may be in the wrong "Control space" but am unsure of how to proceed.

Any help to what I am sure is a simple but frustrating problem would be much appreciated.

Cheers



Thanks in advance,


Steve Lewy
Solutions Developer
SimplyData
simplydata.com.au
(dont cut corners or you'll go round in circles)
 
Steve - probably a dozen ways to do this but I had success with the following code:
Code:
<%@ Page Language="VB"%>
<%@Import Namespace = "Microsoft.VisualBasic"%>
<%@Import Namespace = "System"%>
<%@Import Namespace = "System.Web"%>
<%@Import Namespace = "System.Web.UI"%>
<script runat="server">
Dim i As Integer
Dim cn As String
Sub Page_Load(sender As Object, e As EventArgs)
 If Not IsPostBack Then
  For i = 1 to 4
   cn="ETAC" & i
   ctype(me.findcontrol(cn),textbox).text=10*i
  Next i
 End If
End Sub
</script>
<html>
<head>
<title>Loop Text Test</title>
</head>
<body>
<form id="Form1" runat="server">
<asp:TextBox ID="ETAC1" runat="server"></asp:TextBox>
<asp:TextBox ID="ETAC2" runat="server"></asp:TextBox>
<asp:TextBox ID="ETAC3" runat="server"></asp:TextBox>
<asp:TextBox ID="ETAC4" runat="server"></asp:TextBox>
</form>
</body>
</html>
 
Thank you Isadore. I agree, this does work in a simple form environment.

I am using Visual Studio 2005. My page is constructed using a Master form, which contains its own local controls, and a couple of ContentPlaceHolders into which I then build the controls that I need to reference programmatically. The actual Textboxes are placed on a Wizard control, so hierarchically, they are 3 or four levels down.


I understand that I may have to hierarchically get to the controls, but dont know how to do this.

I hope I have made the situation clearer, and that you can help further.

Thanks in advance,


Steve Lewy
Solutions Developer
SimplyData
simplydata.com.au
(dont cut corners or you'll go round in circles)
 
Incidentally, I forgot to post this; the error that I am getting when I run the page is:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

cheers,

Steve Lewy
Solutions Developer
SimplyData
simplydata.com.au
(dont cut corners or you'll go round in circles)
 
The reason you get that error is because the FindControl method isn't actually finding a control and therefore can't set the text property. The reason it can't find a control is because when you refer to Me, you are referring to the actual page and not the master page.

Try using Me.Master instead (and also remember that the Master page may not actually be the parent either so you may have to refer to the TextBox's parent on the master as well).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks ca8msm. I did figure that it was not finding the control.

The problem is not related to the master page, as the controls I am referring to are located in a ContentPlaceholder into which I place them.

These controls are actually located on a Wizard control in the ContentPlaceHolder, (Wizard1), and I have discovered that they in fact become sub-controls of this control. I am able to access these controls using the wizard properties; for example:

Code:
? wizard1.ActiveStep.Controls(9).ID

yields the name of the 10th control of the active step of the wizard (from within the immediate window).

Iterating through this collection would then yield all of its members.

What I have yet to figure out is how to change the associated Text property of that control;

for example:

Code:
wizard1.ActiveStep.Controls(9).text = 123

Which doesnt work, because wizard1.ActiveStep.Controls(9) does not have a text property associated with it. If you know how to assign using the above as a base, that would be very useful,

Cheers,

Steve Lewy
Solutions Developer
SimplyData
simplydata.com.au
(dont cut corners or you'll go round in circles)
 
Nailed it!

The syntax to assign a value to the 10th control in a wizard container is:

Code:
CType(Wizard1.ActiveStep.Controls(9),TextBox).Text = "2345"

The CType casts the generic control to that of a TextBox, which then makes its Text property available.

Using the above, and iterating through the Wizard1.ActiveStep.Controls collection, its possible to isolate a control "variable" and then do things with it.



Steve Lewy
Solutions Developer
SimplyData
simplydata.com.au
(dont cut corners or you'll go round in circles)
 
Sorry, I didn't mention CType as I noticed you'd already used in in your first example (I also wrongly assumed that the controls you were looking for were on the master page).

Although you've got it working, I think that could be a potentially "flakey" solution. What happens if the control you are after isn't actually rendered as the wizards 10th control? I'm just worried that you'll end up getting a reference to a control that you were not expecting.

What I'd suggest, is using a combination of all the methods that have been discussed. For example, use FindControl with the name of the control you are looking for and then cast it to a TextBox. For example, something like:
Code:
        Dim myTextBox As New TextBox
        myTextBox = TryCast(Wizard1.ActiveStep.FindControl("ETAC1"), TextBox)
        If myTextBox IsNot Nothing Then myTextBox.Text = "2345"

What I've done above is create a new instance of a TextBox and then used FindControl to find it by the relevant ID (you can change the number 1 to be i in your loop as needed). Notice that I've also used something called TryCast as well instead of CType to cast the control to a TextBox. This basically does the same thing but will return Nothing if no control exists whereas CType will actually error. I then set the Text property of the TextBox.

I think this should be a lot more "solid" solution and it won't error if it encounters any problems.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Yes, thanks. I actually had'nt intended to use the code as I had it; it was posted purely to identify that I had identified the appropriate "building blocks".

You have however very nicely "integrated" it into a neat package, which was going to be my next step.

For this much thanks.

Cheers,




Steve Lewy
Solutions Developer
SimplyData
simplydata.com.au
(dont cut corners or you'll go round in circles)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top