why doesn't FindControl also work on user controls?

psykik

New member
Joined
Mar 1, 2005
Messages
2
Programming Experience
1-3
I'm going totally nuts here.
I hope someone can help me here, I've been going nuts for 2 days now.

I have a page where I update and insert items. (for this particular website it's horses.)
Each horse has a lot of properties. I made user controls for all properties.
all user controls contain a simple dropdownlist with inside:
value: an integer (FK to the property table inside the database)
display: the name of the property being read out the db

also before databinding the dropdownlist with values out of the db I've got a hard coded one : "NOT AVAILABLE".
Not available will basically insert NULL inside the database row.

for inserting horses, I simply put an html table inside a panel and put textareas, textbox, and all the user controls there. (I can thus call all these controls directly in my code-behind file)



INSERTING works perfectly. It's the updating part that is making me go nuts:
I now use these user controls as templatefields inside a formview (or detailview, I tried both) for the updating part.

here's a small example:


for inserting, an click-event is fired on the button "new"

all my values are checked or replaced:

VB.NET:
Dim ras As Object = RaceSelect1.SelectedRace
                If Not ras.ToString = String.Empty Then
                    ras = CInt(RaceSelect1.SelectedRace)
                Else
                    ras = sqlintnull
                End If

here if in my usercontrol, I selected a race that is in the db then it will use that value.
if instead "not available" is selected, the value for "not available" is empty and so a variable sqlintnull is entered.

this @ras is then used as a parameter for my stored procedure. I do this for about 15 parameters.

this work perfectly because these controls are not inside a detailview of formview.


for updating (the information is thus already in the db) I want to display it in a formview of detailview. but if the user clicks "edit" he must be able to change the values.
when in edit mode if the user chooses "not available" in the usercontrol dropdownlist I get an invalid cast to error because inside the dropdownlist it's a STRING and not DBNULL.

this is why I need :

VB.NET:
Dim ras As Object = RaceSelect1.SelectedRace
                If Not ras.ToString = String.Empty Then
                    ras = CInt(RaceSelect1.SelectedRace)
                Else
                    ras = sqlintnull
                End If
but here instead as in inserted:

VB.NET:
Dim RaceSelect1 as UserControl = CType(Formview1.FindControl("RaceSelect1"), UserControl)

Dim ras As Object = [B]RaceSelect1.SelectedRace[/B]
                If Not ras.ToString = String.Empty Then
                    ras = CInt(RaceSelect1.SelectedRace)
                Else
                    ras = sqlintnull
                End If
(the bold doesn't work!, I cannot seem to get into my public property meaning it doesn't recognises the user control)

:( :(


ok then I tried this:

I replaced 1 usercontrol with a plain dropdownlist linked to a sqldataconnection.

if I then go to the code behind : (didn't put all the code, just the logic here )


...
VB.NET:
formview1.findcontrol("dropdownlistname")
and then
VB.NET:
dropdownlistname.selectedvalue
it works


so why doesn't this also work on user controls ...

VB.NET:
formview1.findcontrol("usercontrolname")

usercontrolname.publicmethodname

what's the use of usercontrols if this doesn't work :( :(



any help would be great

thank you
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,609
Location
Norway
Programming Experience
10+
UserControl is a system type derived from System.Web.UI.Control and does not have a SelectedRace property. The FindControl method returns a Control (or a higher level control derived from it) by ID. If you have named the usercontrol MyUserControl the first default instance of this added to a page will get the ID MyUserControl1. Your ID "RaceSelect1" indicates that your usercontrol is named "RaceSelect" and this is also its type name. RaceSelect will also be the user defined type that can be used to access the type specific properties, like SelectedRace. Try this code below, substituting RaceSelect with what you called it, it will Cast fine if it is the correct type:
VB.NET:
Dim RaceSelect1 As RaceSelect = Page.FindControl("RaceSelect1")
Dim ras As Object = RaceSelect1.SelectedRace
 

psykik

New member
Joined
Mar 1, 2005
Messages
2
Programming Experience
1-3
thank you for your reply ;)

I have found my stupid mistake. I was trying to cast it to user control instead of specifying the name of my usercontrol within the correct namespace.
I casted it to the correct named usercontrol and now it works beautifully ;)


I was a bit confused there ;) it's because I've seen most casts to textbox or dropdownlists and so without thinking I tried to cast it to USERCONTROL.


ok now I'm finally "freed" !

w00T I can continu now ;)

thanks
 
Top Bottom