472,794 Members | 2,191 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,794 software developers and data experts.

textbox names equal to variables

ar
Hi,

Simple question... I have a form with 3 textboxes : txt1, txt2,txt3

I have the names of those 3 textboxes stored in a db with their
cooresponding values.
txt1 , "test1"
txt2, "test2"
txt3, "test3"

My question is if I pull the 1st entry from the db, I read 'txt1' into a
variable 'x'. How do I then say x.text = "whatever" (so that I'm really
saying txt1.text = "whatever") I hope this makes sense and isn't too simple
of a question.
Jul 21 '05 #1
7 2158
Hi ar,

Well, you could loop through all your TextBoxes and grab the one you need.
Beware that the reference name cannot be used so you will have to store
'txt1' etc in either the Control.Name property or Control.Tag

string s = "txt1";
string t = "test1";
TextBox tb = null;
foreach(Control c in this.Controls)
{
if(c.Name == s)
tb = (TextBox)c;
}

tb.Text = t;
--
Happy Coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #2
ar
Hi Morten,

Thanks for the reply. For some reason when I run the code, I get 'Name' is
not a member of 'System.Web.UI.Control'

Any ideas? I know it's something simple....

"Morten Wennevik" wrote:
Hi ar,

Well, you could loop through all your TextBoxes and grab the one you need.
Beware that the reference name cannot be used so you will have to store
'txt1' etc in either the Control.Name property or Control.Tag

string s = "txt1";
string t = "test1";
TextBox tb = null;
foreach(Control c in this.Controls)
{
if(c.Name == s)
tb = (TextBox)c;
}

tb.Text = t;
--
Happy Coding!
Morten Wennevik [C# MVP]

Jul 21 '05 #3
Well, you didn't specify web so I thought you meant Windows controls.
There is no Name or Tag property in the Web controls.

You may be able to overcome this by keeping the references in an array
along with their names, or something like that.

struct Item
{
public TextBox tb;
public string Name;
public Item(TextBox t, string s)
{
tb = t;
Name = s;
}
}

ArrayList list = new ArrayList()
list.Add(new Item(txt1, "txt1");
list.Add(new Item(txt2, "txt2");
list.Add(new Item(txt3, "txt3");

foreach(Item i in list)
{
if(i.Name == "txt1")
{
i.tb.Text = "test1";
break;
}
}

PS! code is not tested

--
Happy Coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #4
Ar,

Are you using VBNet or C#?
(Than I can make a little sample how to catch this easy).

Cor
Jul 21 '05 #5
ar
VB.NET

"Cor Ligthert" wrote:
Ar,

Are you using VBNet or C#?
(Than I can make a little sample how to catch this easy).

Cor

Jul 21 '05 #6
ar,

This little (tested) sample needs on a webpage three textboxes with ID
Test1, Test2, Test3

\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim frm As Control = Me.FindControl("Form1")
Dim test() As String = {"Whatever1", "Whatever2", "Whatever3"}
For Each ctr As Control In frm.Controls
If Not ctr.ID Is Nothing Then
If ctr.ID.Substring(0, 4) = "Test" Then
DirectCast(ctr, TextBox).Text = _
test(CInt(ctr.ID.Substring(4, 1)) - 1)
End If
End If
Next
End Sub
///
I hope this helps?

Cor
Jul 21 '05 #7
ar
Thanks!

"Cor Ligthert" wrote:
ar,

This little (tested) sample needs on a webpage three textboxes with ID
Test1, Test2, Test3

\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim frm As Control = Me.FindControl("Form1")
Dim test() As String = {"Whatever1", "Whatever2", "Whatever3"}
For Each ctr As Control In frm.Controls
If Not ctr.ID Is Nothing Then
If ctr.ID.Substring(0, 4) = "Test" Then
DirectCast(ctr, TextBox).Text = _
test(CInt(ctr.ID.Substring(4, 1)) - 1)
End If
End If
Next
End Sub
///
I hope this helps?

Cor

Jul 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sugapablo | last post by:
I have a dynamic form that that creates input variables named different things based on what it reads from the database. So, for example, it may create three fields, named ex12, ex23, and ex45...
6
by: Oren | last post by:
Hi, Is there a way to use the SelStart functionality in a textbox that has more than 32,767 characters in it, when the cursor is beyond the 32,767th characterz? SelStart is an integer, and it...
6
by: JohnR | last post by:
I have a table with 1 row which is used to hold some application wide items (one item per field, hence I only need 1 row). I want to bind one of the fields to a textbox. After setting up the...
7
by: ar | last post by:
Hi, Simple question... I have a form with 3 textboxes : txt1, txt2,txt3 I have the names of those 3 textboxes stored in a db with their cooresponding values. txt1 , "test1" txt2, "test2"...
5
by: Drew | last post by:
I am building an application for inserting and updating one field in a database. The database is in SQL Server and I am using Classic ASP. I have a dropdown listbox that is populated with names,...
0
by: Allan Kim Jensen | last post by:
Hello, I am trying to retrieve data from a MySql database using ASP.net, and it works fine when putting the result into a gridview (Code provided in the end of this mail). However, I'd like...
17
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are...
1
by: cyningeston | last post by:
OS: WinXP Pro, VB/ASP/ADO.NET I'm building a web-based supplier management application. For each supplier we are required by the FDA to track certain documents. I've managed to pull them from...
1
by: leiger | last post by:
Hi, I need help with this problem as soon as possible (within a couple of days). This is the first time I have ever used Access 2007 and therefore I am having some problems - especially as I...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.