referencing a control dynamicaly

wessamzeidan

Junior Contributor
Joined
Dec 8, 2002
Messages
379
Location
Lebanon
If I have the name of a control in a string, how can I reference that control using the string....
for example

Dim x As String="textbox1"

I need to use 'x' to reference the textbox with the name textbox1

Any one has any idea??
 
Are you trying to do something like:

VB.Net
Dim Controlname As String = Me.TextBox1.Name

ASP.Net
Dim Controlname As String = Me.TextBox1.ID

Bit hazy on the actual question.
 
Try this:

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim textbox As TextBox
        textbox = FindControl("textbox1")
        If Not textbox Is Nothing Then
        textbox.Text = "Hello world"
        End If

    End Sub

    Private Function FindControl(ByVal Controlname As String) As TextBox

        Dim ctl As Control
        For Each ctl In Me.Controls
            If TypeOf ctl Is TextBox Then
                If ctl.Name.ToUpper = Controlname.ToUpper Then
                    Return ctl
                End If
            End If
        Next

    End Function
 
asp.net

I have about 10 textboxes with names textbox0 to textbox9. I'm reading a number from the querystring, and according to that number I'm setting the text property of a textbox...for example
If I read 1 from the querystring, I should textbox1.text to some value

so instead of making a series of if statments I thought of creating the name of the control dynamicaly and using it to set its text property, thats how I used to do it in Flash MX.....
 
You can use the findcontrol method:

Dim control As TextBox
control = CType(Page.FindControl("textbox1"), TextBox)
control.text = "Hello World"


wessamzeidan said:
asp.net

I have about 10 textboxes with names textbox0 to textbox9. I'm reading a number from the querystring, and according to that number I'm setting the text property of a textbox...for example
If I read 1 from the querystring, I should textbox1.text to some value

so instead of making a series of if statments I thought of creating the name of the control dynamicaly and using it to set its text property, thats how I used to do it in Flash MX.....
 
what I meant, instead of using findcontrol to look for the control in the form, find a way that can be used both in web apps and windows apps...

For example in Flash Actionscript I use this way

eval("textbox"+1)._visible=false;

eval() would evaluate the string into an instance name which I can use to change its properties....
 
This piece of code is nice.. Just think of modifying it to suit your need

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If FindControl("textbox1") = True Then  'you see, only one line
             textbox.Text = "Hello world"   
        End If

    End Sub

    Private Function FindControl(ByVal Controlname As String) As Boolean
    FindControl = False
        Dim ctl As Control
        For Each ctl In Me.Controls
            If TypeOf ctl Is TextBox Then
                If ctl.Name.ToUpper = Controlname.ToUpper Then
                    FindControl = True
                End If
            End If
        Next

    End Function
 
Have you tried this? Declare an array of textboxes and use the index to specify the textbox you want? ie. txtbox(i).Text = "text here".

Not sure if this is what you want though.
 
You are correct that I am indeed new to ASP .Net. But this is doable. I have tested the following example myself:

Code:
Public Class WebForm1
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

    Dim txbx(9) As TextBox

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        txbxInit()
    End Sub

    Private Sub txbxInit()
        Dim i As Integer
        For i = 0 To txbx.Length - 1
            txbx(i) = New TextBox()
            txbx(i).Text = "Textbox No. " & i + 1
            Page.Controls(1).Controls.Add(txbx(i))
        Next
    End Sub

    Sub changeText(ByVal index As Integer, ByVal newText As String)
        If index >= 0 And index < txbx.Length Then
            txbx(index).Text = newText
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        changeText(3, "Hello World")
    End Sub
End Class

Please note that I did not add any event handlers. I am just showing you that this is possible.

But if this is something that you are not supposed to do under .Net, because its not recommended by MS or something, please let me know. Many thanks in advance.
 
Last edited:
wessamzeidan said:
eval("textbox"+1)._visible=false;

eval() would evaluate the string into an instance name which I can use to change its properties....
(NB if you are not sure that the control will be found, wrap in exception handlers )

in C#:

FindControl("textbox"+1).visible = false;

need to set a more specific property? just cast it:

((TextBox) FindControl("textbox"+1)).Text = "New Text";
 
this works in a webform only, not in a windows form. I was looking for a way that can be used in both types of applications.....any way thanks
 
Back
Top