accessing datalist child controls

M

MattB

I think I'm close to making this work, but so far it does not.

I have a datalist (id = "dlVFields") on a web form that has an item template
with a textbox in it. The textbox id = "txtVerify". When the form is
submitted via a button click (button is outside datalist) I loop through the
datalist's DataListItem collection to retrieve any values entered into the
textboxes. My problem is that I'm not getting anything back from the
textboxes when they have had text entered into them. I'm sure it's the way
I'm referencing them.
All the examples I've found are driven by events within the datalist, and
mine is not.
Here's what I'm currently trying:

For Each dlitem In dlVerify.Items

Dim strField As String = dtVFields.Rows.Item(i).Item("Field")

Dim tb As TextBox = CType(dlitem.FindControl("txtVerify"), TextBox)

txtInput = tb.txt

i = i + 1

Next

------------------------

It seems that tb.text is always = "". Can anyone tell me how to correctly
reference that control in this context? Thanks!



Matt
 
S

Scott Hamlin

Hmm, not sure. Does the last TextBox in the DataList have any text? If not,
then maybe the txtInput string is being overwritten. Try this...

txtInput = txtInput & tb.txt

....and see if that works.
 
G

Guest

I think that you are missing something...

Try this

Dim tb As TextBox = CType(dlitem.SelectedItem.FindControl("txtVerify"), TextBox

You need to do the FindControl within the selected item on the list and not the List itself


----- MattB wrote: ----

I think I'm close to making this work, but so far it does not

I have a datalist (id = "dlVFields") on a web form that has an item templat
with a textbox in it. The textbox id = "txtVerify". When the form i
submitted via a button click (button is outside datalist) I loop through th
datalist's DataListItem collection to retrieve any values entered into th
textboxes. My problem is that I'm not getting anything back from th
textboxes when they have had text entered into them. I'm sure it's the wa
I'm referencing them
All the examples I've found are driven by events within the datalist, an
mine is not
Here's what I'm currently trying

For Each dlitem In dlVerify.Item

Dim strField As String = dtVFields.Rows.Item(i).Item("Field"

Dim tb As TextBox = CType(dlitem.FindControl("txtVerify"), TextBox

txtInput = tb.tx

i = i +

Nex

-----------------------

It seems that tb.text is always = "". Can anyone tell me how to correctl
reference that control in this context? Thanks



Mat
 
M

MattB

Thanks, but the variable is empty even if I only have one item in the list.
This was a simplified version of what I'm doing to work this issue out.

Matt
 
M

MattB

Thanks, but I don't think that's it. I'm pretty sure dlItem is already the
selected item because I'm looping for each dlItem (which is of a
DataListItem type) in dlVerify (the Datalist).

Intellisense won't give me a dlitem.SelectedItem option so I tried to do
dlVerify.SelectedItem. Unfortunately that errored out at runtime because
SelectedItem was Nothing. I think that's because the event is not being
created by the DataList, so no one item was ever selected.

Thanks for the suggestions though. I really appreciate it! Any other ideas
based on this information?

Matt
 
G

Guest

Matt, I did a simple test and works for me, only thing is that I'm using C# but that should be a problem, this my code..

private void btnPost_Click(object sender, System.EventArgs e

foreach (DataListItem item in dlFields.Items

TextBox tb = (TextBox)item.FindControl("txtVerify")
string text = tb.Text
}


I ASP.NET contains the DataList and a Button to post the page and look for the values, if you are having problems looking at the child controls I will imagine that when you post the page and get it back to the client, whatever you changed on the text fields shouldn't be there, meaning that the information is not really post to the server and that's why you don't see it. Any way, this is my code, and my list has 2 items, and can change the text box text and post the page and see the value on the server

Let me know!
 
R

Rick Spiewak

Which is the name of the datalist which is changing - dlVFields or
dlVerify??

Also "txtInput = tb.txt" - the property is .Text, not .txt.


Assuming that these are typos, and not the actual problem, it looks like the
code *should* work.

I'm doing something very similar, and it works. (I don't have Option Strict
on, so no CType or DirectCast...)

Dim tbS As TextBox
Dim lb As Label
Dim dS As Decimal
..
..
..

For Each itm As DataListItem In Me.DataListOrder.Items
lb = itm.FindControl("lblProductID")
pID = lb.Text 'Get the ProductID (overwritten for new entries)

..
..
..
' Set shipping value for existing rows, it is allowed to
change
tbS = itm.FindControl("txtShipping")
If IsNumeric(tbS.Text) Then
dS = CDec(tbS.Text)
ElseIf tbS.Text = "" Then 'If blank, it's 0
dS = 0
End If
..
..
..
 

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,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top