accessing datalist child controls

Discussion in 'ASP .Net' started by MattB, Apr 20, 2004.

  1. MattB

    MattB Guest

    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
     
    MattB, Apr 20, 2004
    #1
    1. Advertising

  2. MattB

    Scott Hamlin Guest

    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.

    "MattB" <> wrote in message
    news:c63m2u$7olds$-berlin.de...
    > 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
    >
    >
     
    Scott Hamlin, Apr 20, 2004
    #2
    1. Advertising

  3. 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
     
    =?Utf-8?B?TVIuIFVOREVSSElMTA==?=, Apr 20, 2004
    #3
  4. MattB

    MattB Guest

    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

    Scott Hamlin wrote:
    > 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.
    >
    > "MattB" <> wrote in message
    > news:c63m2u$7olds$-berlin.de...
    >> 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
     
    MattB, Apr 21, 2004
    #4
  5. MattB

    MattB Guest

    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

    MR. UNDERHILL wrote:
    > 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 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
     
    MattB, Apr 21, 2004
    #5
  6. MattB

    Rick Spiewak Guest

    Where are you doing this? In the page load event, or the button click event?

    "MattB" <> wrote in message
    news:c63m2u$7olds$-berlin.de...
    > 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
    >
    >
     
    Rick Spiewak, Apr 22, 2004
    #6
  7. MattB

    MattB Guest

    Buton click. Got an idea? Thanks for responding!

    Matt

    Rick Spiewak wrote:
    > Where are you doing this? In the page load event, or the button click
    > event?
    >
    > "MattB" <> wrote in message
    > news:c63m2u$7olds$-berlin.de...
    >> 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
     
    MattB, Apr 22, 2004
    #7
  8. 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!
     
    =?Utf-8?B?TVIuIFVOREVSSElMTA==?=, Apr 23, 2004
    #8
  9. MattB

    Rick Spiewak Guest

    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
    ..
    ..
    ..
    "MattB" <> wrote in message
    news:c68lpt$950v5$-berlin.de...
    > Buton click. Got an idea? Thanks for responding!
    >
    > Matt
    >
    > Rick Spiewak wrote:
    > > Where are you doing this? In the page load event, or the button click
    > > event?
    > >
    > > "MattB" <> wrote in message
    > > news:c63m2u$7olds$-berlin.de...
    > >> 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

    >
    >
    >
     
    Rick Spiewak, Apr 23, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TW9vampvbw==?=
    Replies:
    0
    Views:
    461
    =?Utf-8?B?TW9vampvbw==?=
    Jul 8, 2005
  2. Jeff Rodriguez
    Replies:
    23
    Views:
    1,210
    David Schwartz
    Dec 9, 2003
  3. Moldy

    Accessing Properties of Custom Controls child Controls

    Moldy, Sep 4, 2003, in forum: ASP .Net Building Controls
    Replies:
    8
    Views:
    241
    Moldy
    Sep 5, 2003
  4. Replies:
    0
    Views:
    220
  5. Moldy
    Replies:
    8
    Views:
    263
    Moldy
    Sep 5, 2003
Loading...

Share This Page