Access text box using variable

  • Thread starter Thread starter flash
  • Start date Start date
F

flash

Hi,

I am trying to access a text control using a variable name rather than a
fixed id.

ie I have a text controls called "tx1" "tx2" "tx3" etc. and i want to set
the control using
something like

"id" & variable

rather than "id1"

Is there a way in asp.net i can access a text control using the DOM so don't
have to have a hardcoded id in my code?

TIA.
 
Sure, both on server and client side you can declare variables and assign to
them values:

TextBox tb;
tb = tx1;
tb.Text = "whatever";

Eliyahu
 
Sure, both on server and client side you can declare variables and assign to
them values:

TextBox tb;
tb = tx1;
tb.Text = "whatever";

Eliyahu

Um, you've lost me a bit there Eliyahu.

if I have

<asp:textbox id="St1" runat=server/>

Then how would I access the textbox in serverside vb.net without using its
name of ST1 but using avaraible set to "ST1".

Cheers.
 
I think this might help:

private void Button1_Click(object sender, EventArgs MyEventArgs)
{
// Find control on page.
Control myControl1 = FindControl("TextBox2");
if(myControl1!=null)
{
// Get control's parent.
Control myControl2 = myControl1.Parent;
Response.Write("Parent of the text box is : " +
myControl2.ID);
}
else
{
Response.Write("Control not found");
}
}

More info:
http://msdn.microsoft.com/library/d...rfsystemwebuicontrolclassfindcontroltopic.asp

Cheers
Remy Blaettler
www.collaboral.com
 
Is not it what I wrote? tb is the variable, you set it to ST1, whatever vb
syntax is (something like 'dim tb' and 'tb=ST1' ? I don't write in vb), and
use tb.Text instead of ST1.Text.

Eliyahu
 
Remy said:
I think this might help:

private void Button1_Click(object sender, EventArgs MyEventArgs)
{
// Find control on page.
Control myControl1 = FindControl("TextBox2");
if(myControl1!=null)
{
// Get control's parent.
Control myControl2 = myControl1.Parent;
Response.Write("Parent of the text box is : " +
myControl2.ID);
}
else
{
Response.Write("Control not found");
}
}

More info:
http://msdn.microsoft.com/library/d...rfsystemwebuicontrolclassfindcontroltopic.asp

Thanks, just what I needed. Final code is

dim x as string
x="st" & 0
dim y as textbox
y = FindControl(x)
y.text="hello"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top