Array of TextBoxes

spke711

Member
Joined
Nov 29, 2006
Messages
12
Programming Experience
Beginner
Ok. I have an unfixed-length array of textboxes that I created globally:

VB.NET:
Dim CF_array() As TextBox

In one function, I used ReDim on the array to fix the length according to a local integer:

VB.NET:
Public Function function1()
    Dim someInt As Integer
    someInt = 4
 
    ReDim CF_array(someInt)
    'Then fill the array with textboxes and display on web form with Dynamic Controls
End Function

In another function, I need to get the values of the textboxes. Here's what I tried:

VB.NET:
Public Function function2()
    Dim CF_string As String
    Dim anInt As Integer
    anInt = 0
 
    CF_string = CF_array(anInt).Text
End Function

There seems to be a problem with "CF_string = CF_array(anInt).Text". I get the following error:

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

My guess is that when I ReDim and fill the array in the first function, it's not updating the global variable. So in the second function where I try to get values out of the global array, it's still empty. Any ideas? I think I'm on hour# 4 or 5 of trying to figure this out! Thanks in advance.
 
In your global statement you're using Dim try Public
 
hmmm....I just tried Public and got the same error. If you need actual code just let me know and I can snippet it out because all code from the first post is just sample.
 
ok, here's the live code. the first section is where I create the global array:

VB.NET:
Dim myquery As String             
Dim holder As String             
Dim DS As System.Data.DataSet = New System.Data.DataSet()             
Dim CF_tb() As TextBox             
Dim CF_length as Integer
Here's a section of the first function including the ReDim of the array and adding dynamic controls:

VB.NET:
myReader = myCommand3.ExecuteReader()
 
Do While myReader.Read()                     
    CF_length = myReader("length")                 
Loop                                  

myReader.Close()                                  

myReader = myCommand2.ExecuteReader()                                  

Dim CF_counter As Integer                 
Dim CF_tb(CF_length) As TextBox                 
CF_counter = 0                                  

Do While myReader.Read()                     
   If myReader("fieldName") IS DBNull.Value THEN                         
      'Do nothing                     
   ELSE                         
      Dim lab as Label = New Label()                         
      lab.Text = myReader("fieldName")customHolder.Controls.add(lab)
   End If

If myReader("data") IS DBNull.Value THEN                         
   'Do nothing                     
ELSE                         
   CF_tb(CF_counter) = New TextBox()                         
   CF_tb(CF_counter).Text = myReader("data")                         
   CF_tb(CF_counter).ID = "CF" & myReader("fieldID")
   customHolder.Controls.add(CF_tb(CF_counter))
End If

CF_counter += 1
And finally here's some code out of the second function where I attempt to get values out of the array:

VB.NET:
Dim counter2 As Integer                                  
Dim myReader3 As SqlDataReader = myCommand3.ExecuteReader()

For counter2 = 0 to CF_length                     
   Do While myReader3.Read()                         
      Dim id_str As String                         
      id_str = "CF" & myReader3("fieldID")                         

      If CF_tb(counter2).ID = id_str then                         
             If CF_tb(counter2).Text = myReader3("data") then                                 
            Do nothing                         
          Else                         
                   updateCustomField(CF_tb(counter2).Text)                         
          End If                         
      End If                          
   Loop                 
Next counter2                 

myReader3.Close()
Sorry if the code looks a little screwed up. I had some trouble with copy and paste (says a lot for my programming skills!):p
 
ok first off when you say you are dimming globally I'm assuming your creating a module and placing them there?

If you want to share variable throughout your entire project you must create a module and place your dims inside of it.
 
Last edited:
well, i only need that variable on one page. by dimming globally i mean that i am dimming it right after <script runat="server"> instead of inside a SUB or Function. is this not correct?
 
i'm noticing that this is an asp project (i moved it to the webforms forum), every time the page refreshes (even with the data results) everything is redrawn, even the array. i would try using a module create the array
VB.NET:
'module
Friend CF_tb() As TextBox
Friend CF_length as Integer
 
Putting the variables in a module worked...but i have another (hopefully quick) question. Is it possible to get a textbox value like this:

Dim textBox_string As String
textBox_string = "f8888"

textBox_string.Text

...

the textBox would look like <asp:TextBox id="f8888" runat="server">

I know that exact code won't work, but is there some other way to do it similarly? or do i have to actually use f8888.Text? Thanks for everyone's help, I really appreciate it!
 
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] c [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control = Page.FindControl([/SIZE][SIZE=2][COLOR=#800000]"f8888"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]If [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] c [/SIZE][SIZE=2][COLOR=#0000ff]Is [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff] Dim[/COLOR][/SIZE][SIZE=2] t [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] TextBox = [/SIZE][SIZE=2][COLOR=#0000ff]DirectCast[/COLOR][/SIZE][SIZE=2](c, TextBox)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
 
hmmm, the page.findcontrol(string) didnt work. i think i know why though. the textboxes are dynamically created using literal controls and placed in the web form at a placeholder. when the update button is clicked the page refreshes and im pretty sure the literal controls are all cleared out before i can get the values from the textboxes. ive been researching this for a little while and haven't found a workaround yet.
 
Thanks for the link Paszt. The concept is a little confusing to me, but I'll look over the code more and tinker around with it in my own project.
 
Using FindControl method on the Placeholder controls collection works fine here, example code with a Placeholder and a Button on a asp.net page:
VB.NET:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.Load
        Dim t As New TextBox
        t.ID = "myTB"
        PlaceHolder1.Controls.Add(t)
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Button1.Click
        Dim c As Control = PlaceHolder1.FindControl("myTB")
        If Not c Is Nothing Then
            Dim t As TextBox = DirectCast(c, TextBox)
            Page.Title = t.Text
        End If
    End Sub
 
For some reason I'm getting the following compilation error:

Compiler Error Message: BC30183: Keyword is not valid as an identifier.

Source Error:


Line 15:
Line 16: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Line 17: Handles Me.Load
Line 18: If Page.IsPostBack Then
Line 19: If editDonationPanel.visible = true Then


I honestly don't know why...I'm using ASP.NET 1.1....maybe that's why?
 
Back
Top