471,348 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 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 2086
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Sugapablo | last post: by
6 posts views Thread by Oren | last post: by
7 posts views Thread by ar | last post: by
reply views Thread by Allan Kim Jensen | last post: by
1 post views Thread by Ronak mishra | last post: by

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.