FindControl fails in event handler for usercontrol

Discussion in 'ASP .Net' started by Bill E., Dec 9, 2009.

  1. Bill E.

    Bill E. Guest

    ASP.NET 3.5
    I have a repeater generating table rows containing a hiddenfield, a
    simple control (textbox) and a usercontrol (mycontrol). I am using
    the repeater ItemCreated event to attach event handlers for the
    textbox and the usercontrol:

    Protected Sub rpt1_ItemCreated(ByVal obj As Object, ByVal e As
    RepeaterItemEventArgs)
    'Add event handlers

    Dim tbValue As TextBox = DirectCast(e.Item.FindControl
    ("tbValue"), TextBox)
    AddHandler tbValue.TextChanged, AddressOf ValueChanged


    Dim dtValue As ASP.controls_mycontrol_ascx = DirectCast
    (e.Item.FindControl("dtValue"), ASP.controls_mycontrol_ascx)
    AddHandler dtValue.Changed, AddressOf ValueChanged

    End Sub

    The event handler ValueChanged is firing correctly, but when it fires
    for the usercontrol, I am unable to use FindControl to locate the
    hidden field. When it fires for the textbox, the hidden field is
    found successfully:

    Protected Sub ValueChanged(ByVal obj As Object, ByVal e As
    EventArgs)
    'Handle changes to control values

    'Get the identifier of the element
    Dim FieldID As Integer
    Try
    Dim tbid As HiddenField = obj.FindControl("tbid")
    FieldID = Convert.ToInt32(tbid.Value)
    Catch ex As Exception
    'This is done because we can't seem to use FindControl to
    find tbid from the datebox.
    Response.Write("Can't find the hidden field")
    End Try
    End Sub

    Does anyone know why FindControl is failing when the event fires for
    the usercontrol but not for the textbox?

    Thanks,

    Bill E.
    Hollywood, FL
    Bill E., Dec 9, 2009
    #1
    1. Advertising

  2. "Bill E." <> wrote in news:75d0278e-355f-48dc-
    :

    > Does anyone know why FindControl is failing when the event fires for
    > the usercontrol but not for the textbox?


    Not off hand. I would imagine it is not in e for some reason, but that
    is just a wild guess without playing.

    But that may not be the problem here. You say this is a hidden textbox
    or soemthing? What is it used for?

    The reason I ask is hidden is normally used to persist something on the
    client (to make things easier), but it is really not a good methodology
    in ASP.NET. At least not in MOST cases.

    By understanding the application, we may be able to think up a better
    solution that avoids this issue.

    Peace and Grace,

    --
    Gregory A. Beamer (MVP)

    Twitter: @gbworld
    Blog: http://gregorybeamer.spaces.live.com

    *******************************************
    | Think outside the box! |
    *******************************************
    Gregory A. Beamer, Dec 10, 2009
    #2
    1. Advertising

  3. Bill E.

    Bill E. Guest

    Gregory,

    The HiddenField stores an identifier that is later used in an update
    statement. I have already come up with a workaround which is to store
    the identifier in a property of the usercontrol, thus avoiding the
    issue. Nevertheless, I still want to understand why FindControl isn't
    working when the usercontrol fires the event.

    Bill
    Bill E., Dec 11, 2009
    #3
  4. "Bill E." <> wrote in news:066ed35b-c628-4bb8-
    :

    > The HiddenField stores an identifier that is later used in an update
    > statement. I have already come up with a workaround which is to store
    > the identifier in a property of the usercontrol, thus avoiding the
    > issue. Nevertheless, I still want to understand why FindControl isn't
    > working when the usercontrol fires the event.


    I will have to play with that at some time. In general, I keep the
    persisted data on the server side and use ID to look it up rather than
    use hidden fields.

    Hidden fields were very popular about 10 years ago, and some still use
    them. Unfortunately, they are hackable. In most cases, it is not a huge
    deal, but there were online retailers selling televisions for a penny
    because they put the price in a hidden field. I prefer to eliminate
    hacking ability by only allowing the user to edit fields he should have
    access to.

    Peace and Grace,

    --
    Gregory A. Beamer (MVP)

    Twitter: @gbworld
    Blog: http://gregorybeamer.spaces.live.com

    *******************************************
    | Think outside the box! |
    *******************************************
    Gregory A. Beamer, Dec 11, 2009
    #4
  5. Bill E.

    Bill E. Guest

    The value of the hidden field is different for each row generated in
    the repeater. I would have to store an array for the values. There
    is no danger of hacking the hidden fields in this case.

    Bill
    Bill E., Dec 11, 2009
    #5
  6. Bill E.

    Bill E. Guest

    There was a simple solution to this problem. I simply added the
    following to the definition of the UserControl (in VB.NET)

    Public Overrides Function FindControl(ByVal id As String) As
    System.Web.UI.Control
    Return Me.Parent.FindControl(id)
    End Function

    Bill E.
    Hollywood, FL
    Bill E., Jan 8, 2010
    #6
    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. Replies:
    1
    Views:
    672
    Damien
    Feb 22, 2007
  2. =?Utf-8?B?QWxleCBNYWdoZW4=?=

    FindControl() on ASP.NET UserControl Objects

    =?Utf-8?B?QWxleCBNYWdoZW4=?=, May 13, 2007, in forum: ASP .Net
    Replies:
    14
    Views:
    960
    Steven Cheng[MSFT]
    May 24, 2007
  3. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    163
  4. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    173
    vatech1993
    Dec 11, 2004
  5. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    137
Loading...

Share This Page