How do I cast something as a user control?

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Alan Silver (alan-silver_at_nospam.thanx)
Date: 02/22/05


Date: Tue, 22 Feb 2005 19:52:46 +0000

hello,

If I capture the OnItemDataBound event for a repeater, datalist, etc, I
can get hold of a control in the ItemTemplate like this ...

Literal litFred = (Literal)e.Item.FindControl("litFred");

How do I do this when the ItemTemplate contains a user control? Say I
have created a control, and I add a page directive ...

<%@ Register TagPrefix="MyControls" TagName="Fred" Src="Fred.ascx" %>

I can create the control like this ...

<MyControls:Fred ID="fred" ... />

How do I get hold of this in the OnItemDataBound event? I can't do ...

Fred litFred = (Fred)e.Item.FindControl("litFred");

as this generates an error. Nor can I do ...

MyControls:Fred litFred =
(MyControls:Fred)e.Item.FindControl("litFred");

I even tried ...

MyControls.Fred litFred =
(MyControls.Fred)e.Item.FindControl("litFred");

But that didn't work either.

So, I've tried everything I can think of, please will someone put me out
of my misery and tell me how to do it ;-)

TIA

-- 
Alan Silver
(anything added below this line is nothing to do with me)


Relevant Pages