Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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??

Proudly a Palestinian

Microsoft ASP.NET MVP

My Blog: wessamzeidan.net

Posted

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.

Posted

Try this:

 

   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

Posted

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.....

Proudly a Palestinian

Microsoft ASP.NET MVP

My Blog: wessamzeidan.net

Posted

You can use the findcontrol method:

 

Dim control As TextBox

control = CType(Page.FindControl("textbox1"), TextBox)

control.text = "Hello World"

 

 

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.....

Posted

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....

Proudly a Palestinian

Microsoft ASP.NET MVP

My Blog: wessamzeidan.net

Posted

This piece of code is nice.. Just think of modifying it to suit your need

 

    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

George C.K. Low

Posted

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.

Posted (edited)

You are correct that I am indeed new to ASP .Net. But this is doable. I have tested the following example myself:

 

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.

Edited by bob01900
Posted

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";

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...