How do I cast something as a user control?

  • Thread starter Thread starter Alan Silver
  • Start date Start date
A

Alan Silver

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
 
Hi Alan:

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 ...

What sort of error? A NullReferenceException?

Note the control ID you specified is "fred" in the markup but you are
searching for "litFred" with find control - is that just a typo?
 
If I capture the OnItemDataBound event for a repeater, datalist, etc, I
What sort of error? A NullReferenceException?

Sorry, I forgot to include the error message. I have shown below a full
example, which references a control I wrote to show dates. This is
basically three drop downs, for day, month and year. I want to pull
dates out of the database and display them in the custom control in the
repeater.

When I run this code, I get an error "The type or namespace DatePicker
could not be found (are you missing a using directive or an assembly
reference?)"

Here is the code ...

<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ Register TagPrefix="Pixata" TagName="DatePicker"
Src="DatePicker.ascx" %>

<script runat="server">
void Page_Load(Object sender, EventArgs e) {
if (!IsPostBack) {
SqlConnection conPubs;
SqlCommand cmdSelect;
SqlDataReader dtrAuthors;

// Retrieve records from database
conPubs = new SqlConnection("CONNECTIONSTRINGCHANGED");
cmdSelect = new SqlCommand( "Select * From events", conPubs );
conPubs.Open();
dtrAuthors = cmdSelect.ExecuteReader();

// Bind to Repeater
rptAuthors.DataSource = dtrAuthors;
rptAuthors.DataBind();

dtrAuthors.Close();
conPubs.Close();
}
}

void Repeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
DatePicker lblAuthor = (DatePicker)e.Item.FindControl("dtpDate2");
lblAuthor.DateTimeDT = (DateTime)DataBinder.Eval( e.Item.DataItem, "eventdate" );
}
}
</script>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Test of my date picker custom control</title>
</head>

<body>
<form runat="server">
<asp:Repeater ID="rptAuthors" Runat="Server" OnItemDataBound="Repeater_ItemDataBound">
<ItemTemplate>
<li><Pixata:DatePicker ID="dtpDate2" StartYear="2000" EndYear="2010" Runat="Server" /></li>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>

It complains on the line where I use FindControl. I have tried various
things there, but none work.
Note the control ID you specified is "fred" in the markup but you are
searching for "litFred" with find control - is that just a typo?

Yup, it was air code. I was making up a simple example rather than
chopping down my real one. Sorry about that. The example above should be
more accurate ;-)

Thanks for the reply. Any further help greatly appreciated.
 
When I run this code, I get an error "The type or namespace DatePicker
could not be found (are you missing a using directive or an assembly
reference?)"

Here is the code ...

<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ Register TagPrefix="Pixata" TagName="DatePicker"
Src="DatePicker.ascx" %>

I believe you'll just need another @ Import directive to bring in the
namespace where DatePicker lives. Some namespaces are provided by
default but any classes you write will need thier namespaces brought
into scope.

Make sense?
 
When I run this code, I get an error "The type or namespace DatePicker
I believe you'll just need another @ Import directive to bring in the
namespace where DatePicker lives. Some namespaces are provided by
default but any classes you write will need thier namespaces brought
into scope.

Make sense?

Erm, partly. At the moment, the user control is totally contained in one
..ascx file, so I guess I don't have a namespace for it. How do I go
about creating one?

Please explain this as if you were talking to an idiot!! I'm still
finding my feet here. Thanks for the reply.
 
I believe you'll just need another @ Import directive to bring in the
namespace where DatePicker lives. Some namespaces are provided by
default but any classes you write will need thier namespaces brought
into scope.

Well, after posting, I was digging around in my trusty companion
"ASP.NET Unleashed", which seems to have the answer to everything
(although I often don't realise until afterwards!!), and I found that
all I needed to do was add ...

<%@ Classname="DatePicker" %>

as the first line of the .ascx file. That allowed me to cast the control
directly as a DatePicker ...

DatePicker dtpDate2 = (DatePicker)e.Item.FindControl("dtpDate2");
dtpDate2.DateTimeDT = (DateTime)DataBinder.Eval( e.Item.DataItem, "eventdate" );

Which is exactly what I wanted!!

Thanks again for your help.
 
Back
Top