Why can't I set the textbox "text" property??

M

mj.redfox.mj

Can anyone help? I have a textbox which I'm programatically adding by
using the following code:


txtTest = New TextBox
txtTest.ID = "txtLeft" + cntCount.ToString
Page.FindControl("tdInput").Controls.Add(txtTest)


This successfully creates a textbox called "txtLeft1" in the table
cell "tdInput". Now the next thing I'm doing is this:


Dim txtLeft1 As TextBox
txtLeft1 = Page.FindControl("txtLeft1")
txtLeft1.Text = "Computer"
txtLeft1.BackColor = Color.AliceBlue


....which I'm wanting to change the value of the text in the textbox to
"Computer". However, for some reason it doesn't seem to be working. I
know it's almost certainly something really, really obvious but I just
can't see what it is. I included the last line to change the back
colour, just to check that the textbox control is being found by the
script, and it is indeed. The problem is that it just will not set the
text.


Things I've tried so far that either don't work or are spat out by
Visual Studio are:

txtLeft1.Value = "Computer"

txtLeft1.Text.Value = "Computer"

txtLeft1.Text = "Computer".ToString

txtLeft1.Text = "Computer".ToCharArray

txtLeft1.Text = "Computer"
txtLeft1.DataBind()


If anyone can spot the glaringly obvious here, I'd be immensely
grateful!
 
R

Ray Costanzo

Your code works for me.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim txtTest As TextBox
Dim cntCount As Integer = 1
txtTest = New TextBox
txtTest.ID = "txtLeft" + cntCount.ToString
Page.FindControl("tdInput").Controls.Add(txtTest)
Dim txtLeft1 As TextBox
txtLeft1 = Page.FindControl("txtLeft1")
txtLeft1.Text = "Computer"
txtLeft1.BackColor = Drawing.Color.AliceBlue
End Sub


May I suggest that instead of creating a textbox, adding it, and then
creating a new instance of a textbox that is the same one, you just set the
text and color when you create the initial textbox?


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim txtTest As TextBox
Dim cntCount As Integer = 1
txtTest = New TextBox
txtTest.ID = "txtLeft" + cntCount.ToString
txtTest.BackColor = Drawing.Color.AliceBlue
txtTest.Text = "Computer"
Page.FindControl("tdInput").Controls.Add(txtTest)
End Sub

Ray at work
 
M

mj.redfox.mj

Thanks Ray, at least you've shown I wasn't too far off the right track
I guess!

I'll give that a shot.
 
R

Ray Costanzo

Not at all. :]

Ray at work

Thanks Ray, at least you've shown I wasn't too far off the right track
I guess!

I'll give that a shot.
 
M

mj.redfox.mj

Hmmmm. The textboxes (there are a few of them created by a FOR...NEXT
loop) are actually behaving VERY weirdly, using your suggestion I can
now assign a text value to all but the first two, even though the loop
handles them all identically!!????

I'll have another look tomorrow and then report back as this may be
useful for other people in the future. I wonder if dynamic generated
form elements are just unstable in general?


Not at all. :]

Ray at work


Thanks Ray, at least you've shown I wasn't too far off the right track
I guess!

I'll give that a shot.

Your code works for me.\- Hide quoted text -

- Show quoted text -
 
R

Ray Costanzo

It's possible that what you're trying to do can be done with an entirely
different approach. If you want to post snippets of ASPX and .VB that would
be enough for us to duplicate what you're doing, maybe someone could offer
some alternate routes if they'd seem appropriate.

Ray at work
 
M

mj.redfox.mj

OK, here's the code behind page in full. I didn't post this originally
as it's in a very 'quick and dirty' state at the moment, and needs
tidied, commented and the variables renamed...but this is what I've
been working with It basically adds a variable number of table cells
and textboxes within them, based on the value of 'intAttributes'.
Using this, for some reason I can only set properties of textboxes
txtLeft2 - txtLeft8 (or whatever the upper limit is) - any attempt to
programmatically change the initial text in textboxes txtTest0 or
txtTest1 has no effect. Very, very strange.


Partial Class _ShowData
Inherits System.Web.UI.Page

Sub Page_Load(ByVal Sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strTemplate As String
Dim intAttributes As Integer
Dim txtTest As TextBox
Dim trTest As TableRow
Dim tdTest As TableCell
Dim cntCount As Integer

strTemplate = ""
intAttributes = 1

If Page.IsPostBack Then
If Page.Request.Params.Get("__EVENTTARGET") =
"DropDownList1" Then
strTemplate = DropDownList1.Text
Select Case DropDownList1.Text
Case "Computer"
intAttributes = 8
Case "Generic"
intAttributes = 6
End Select
End If
End If

For cntCount = 1 To intAttributes

trTest = New TableRow
trTest.ID = "trSpecification" + cntCount.ToString
Page.FindControl("tblSpecification").Controls.Add(trTest)

tdTest = New TableCell
tdTest.ID = "tdSpecificationLeft" + cntCount.ToString
Page.FindControl("trSpecification" +
cntCount.ToString).Controls.Add(tdTest)

tdTest = New TableCell
tdTest.ID = "tdSpecificationRight" + cntCount.ToString
Page.FindControl("trSpecification" +
cntCount.ToString).Controls.Add(tdTest)

txtTest = New TextBox
txtTest.ID = "txtLeft" + cntCount.ToString
If strTemplate = "Computer" Then
If cntCount = 0 Then
txtTest.Text = "Test Text 0"
End If
If cntCount = 1 Then
txtTest.Text = "Test Text 1"
End If
If cntCount = 2 Then
txtTest.Text = "Test Text 2"
End If
End If
Page.FindControl("tdSpecificationLeft" +
cntCount.ToString).Controls.Add(txtTest)

txtTest = New TextBox
txtTest.ID = "txtRight" + cntCount.ToString
Page.FindControl("tdSpecificationRight" +
cntCount.ToString).Controls.Add(txtTest)

Next

End Sub
 
M

mj.redfox.mj

QUICK ADD: For some reason, in the above I've accidentally deleted the
line where if 'Computer' is selected in the drop-down list, then
strTemplate is set to "Computer". Sorry about that, the important
point in the above is that the line that adds the text to textbox
txtLeft2 works fine, so I just need to figure out what is so 'special'
about textboxes 1 and 2...
 
M

mj.redfox.mj

I've tried it from 0 though for testing purposes, again couldn't set
the text for that textbox but could for all the ones from 2 onwards.
 
M

mj.redfox.mj

Someone has just told me that I may need to add the textboxes to the
form collection in the Page_Init routine, rather than the Page_Load.

HOWEVER...having just tried this by changing the above code to handle
Page_Init instead, this gives another problem...although it still
recognizes a postback, and even that DropDownList1 is the
'__EVENTTARGET', it can no longer get the VALUE submitted by
DropDownList1 (whereas in Page_Load it can) - and getting that value
is absolutely critical to how this page works. So I'm not sure that
Page_Init is the answer...I'll keep trying though :(
 
D

David

Something else I have just noticed... you can test around this.

You have two if statements around your switch case. If you cannot get past
either of these if statements, then your intAttributes will never be more
than one.

So, ignore the case statement, set specifically intAttributes to a value
afterwards, see what happens. In fact, I was not aware DropDownList had a
..Text value. I always use either .SelectedText, .SelectedValue or
..SelectedIndex. If I am correct, then your intAtrribute value will not be
being set.

You really need to step through your code to see if the values you expect
are correct at certain points. Especially in the loop. Just re-reading your
code again, if I am correct on the DropDownList, then that will ensure that
0, 1 and 2 settings are not being set, as strTemplate will be an empty
string.
--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
 
M

mj.redfox.mj

Hi David

Thanks for replying, you're absolutely correct about those two key
'checkpoints' in the code; the bottom line after doing all sorts of
tests appears to be that if I have the above code in Page_Init and
hard-code the number of textboxes, plus their text values, then it
works, but I can't get the postback submission from the drop down list
(as obviously this info is not available at Page_Init) - if I put the
code in Page_Load, then I DO get the drop-down list postback, but I
believe by this time it's then too late to add the dynamic controls to
the page, as this needs to be done at Page_Init. I guess this is
what's referred to as 'Catch 22'!

Looks like I'm going to have to re-write this whole thing some other
way, which is a real shame as I would have loved to have got this
working dynamically, plus it would have been better coding practice.

It's a classic example of being unable to achieve something in .NET
that would have taken fifteen minutes flat in Classic ASP and
Javascript - very frustrating.
 
M

mj.redfox.mj

Not really, because the value of the dropdown (readable in Page_Load)
determines how many textboxes I need to dynamically create (in
Page_Init). I'm struggling to see a way around this to be honest.
 
M

mj.redfox.mj

I think I may have just stumbled on the answer. It appears the code
needs to be in a DropDownList1_SelectedIndexChanged routine - the
reason I hadn't done this previously was that everything else I'd read
suggested I needed to insert the code in Page_Init - ah well, upwards
and onwards...and thanks for your help along the way guys.
 
D

David

Put the code in a seperate function. Get your selectedindexchanged to call
that function. That way, you can call the function any other time as well
(i.e. what happens when no selectedindexchanged event occurs, for example,
initial load of the page).

--
Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available


I think I may have just stumbled on the answer. It appears the code
needs to be in a DropDownList1_SelectedIndexChanged routine - the
reason I hadn't done this previously was that everything else I'd read
suggested I needed to insert the code in Page_Init - ah well, upwards
and onwards...and thanks for your help along the way guys.
 

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,755
Messages
2,569,536
Members
45,008
Latest member
HaroldDark

Latest Threads

Top