FindControl fails in event handler for usercontrol

B

Bill E.

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
 
G

Gregory A. Beamer

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! |
*******************************************
 
B

Bill E.

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
 
G

Gregory A. Beamer

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! |
*******************************************
 
B

Bill E.

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
 
B

Bill E.

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
 

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

Top