wessamzeidan Posted June 2, 2004 Posted June 2, 2004 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?? Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
jspencer Posted June 2, 2004 Posted June 2, 2004 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. Quote
Administrators PlausiblyDamp Posted June 2, 2004 Administrators Posted June 2, 2004 Are you doing this under windows or asp.net? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jspencer Posted June 2, 2004 Posted June 2, 2004 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 Quote
wessamzeidan Posted June 2, 2004 Author Posted June 2, 2004 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..... Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
jspencer Posted June 2, 2004 Posted June 2, 2004 Are you doing this under windows or asp.net? Presumably vb.net, otherwise he would have used the intrinsic findcontrol method (or would he?). Quote
jspencer Posted June 2, 2004 Posted June 2, 2004 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..... Quote
wessamzeidan Posted June 2, 2004 Author Posted June 2, 2004 Thanks alot, it worked, used findcontrol Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
wessamzeidan Posted June 2, 2004 Author Posted June 2, 2004 but I was wondering, isn't there another, more automatic way to do it???? Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
Administrators PlausiblyDamp Posted June 2, 2004 Administrators Posted June 2, 2004 What do you mean by 'more automatic'? It only took 2 lines of code to get the control in jspencer's post Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
wessamzeidan Posted June 2, 2004 Author Posted June 2, 2004 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.... Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
jspencer Posted June 2, 2004 Posted June 2, 2004 That would be nice. I haven't found any other way of locating a control on a form though. Good luck! Quote
georgepatotk Posted June 3, 2004 Posted June 3, 2004 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 Quote George C.K. Low
wessamzeidan Posted June 3, 2004 Author Posted June 3, 2004 Thanks georgepatotk, I've used the exact same method before, I was just thinking of another way....Thanks again Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
bob01900 Posted June 3, 2004 Posted June 3, 2004 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. Quote
georgepatotk Posted June 3, 2004 Posted June 3, 2004 i think u should be new to .net such thing as text(i).text doesn't exist in .net Quote George C.K. Low
bob01900 Posted June 3, 2004 Posted June 3, 2004 (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 June 3, 2004 by bob01900 Quote
Joe Mamma Posted June 4, 2004 Posted June 4, 2004 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"; Quote 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.
wessamzeidan Posted June 4, 2004 Author Posted June 4, 2004 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 Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.