Referencing textbox

M

Morten Snedker

Im trying to set value of a textbox, but it fails.

txtBox.Text = obj.Effekt does nothing on the page, but the last one
does.

Fieldname is positively correct.

What am I doing wrong?

Dim listNr As String = Right(ListName, 1)
Dim FieldName As String = "txt5" & listNr & "_effekt"

Dim txtBox As New TextBox
txtBox.ID = FieldName

Dim obj As Naturgas
obj = CType(_naturgasList(idx), Naturgas)

txtBox.Text = obj.Effekt
Me.txt51_effekt.Text = obj.Effekt


/Snedker
 
L

Laurent Bugnion [MVP]

Hi,

Morten said:
Im trying to set value of a textbox, but it fails.

txtBox.Text = obj.Effekt does nothing on the page, but the last one
does.

Fieldname is positively correct.

What am I doing wrong?

Dim listNr As String = Right(ListName, 1)
Dim FieldName As String = "txt5" & listNr & "_effekt"

Dim txtBox As New TextBox
txtBox.ID = FieldName

Dim obj As Naturgas
obj = CType(_naturgasList(idx), Naturgas)

txtBox.Text = obj.Effekt
Me.txt51_effekt.Text = obj.Effekt


/Snedker

You create a brand new TextBox, but you don't add it to the Page. It's
just an object which will be deleted as soon as the Page is recycled
(when the Response is sent).

HTH,
Laurent
 
K

Karl Seguin [MVP]

You are never adding the txtBox to the page...you need a placeholder or some
other container on the page..


myPlaceholder.Controls.Add(txtBox);

Karl
 
M

Morten Snedker

On Tue, 23 Jan 2007 08:59:00 -0500, "Karl Seguin [MVP]"

Thanks to the both of you. It seems I haven't explained myself
properly:

I have txt51_effekt, txt52_effekt..txt55_effekt

What I'm trying to do is to reference these dynamically, not creating
new ones. So I guess I got it all wrong. :-\

So, how do I reference each of them dynamically?

Regards /Snedker
 
K

Karl Seguin [MVP]

TextBox txt = Page.FindControl(string.Format("txt{0}_effket", _someId)) as
TextBox;
if (txt == null)
{
//something bad happened
}


Karl
 
L

Laurent Bugnion [MVP]

Hi,
TextBox txt = Page.FindControl(string.Format("txt{0}_effket", _someId))
as TextBox;
if (txt == null)
{
//something bad happened
}


Karl

Careful to translate that to VB.NET before compiling, though ;-)

Laurent
 
M

Morten Snedker

On Tue, 23 Jan 2007 10:12:05 -0500, "Karl Seguin [MVP]"

Worked like a charm! Thanks a bunch to each of you for your help!

Regards /Snedker
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top