Help with UserControl

E

Edgardo Rossetto

Hello there,

I have inserted a UserControl within a DataList:

/*------------------------------------------------------------*/
<asp:DataList ID="DataList1" runat="server" DataKeyField="ID"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<uc1:WebUserControl ID="WebUserControl1" Title="My Title" runat="server" />
</ItemTemplate>
/*------------------------------------------------------------*/

The User Control is very simple, it only has a Literal control:

/*------------------------------------------------------------*/
public partial class WebUserControl : System.Web.UI.UserControl
{
string title = "Untitled";

public string Title
{
get
{
return title;
}

set
{
title = value;
}
}

protected void Page_Load(object sender, EventArgs e)
{
Literal1.Text = title;
}
}
/*------------------------------------------------------------*/

This works just fine, I see "My title" rendered for each Item in the
DataList, however:

/*------------------------------------------------------------*/
<asp:DataList ID="DataList1" runat="server" DataKeyField="ID"
DataSourceID="SqlDataSource1">
<ItemTemplate>
<uc1:WebUserControl ID="WebUserControl1" Title='<%# Eval("Title") %>'
runat="server" />
</ItemTemplate>
/*------------------------------------------------------------*/

This DOESN'T WORK!, all I get is "Untitled" (the default value) :(

I really don't get this behavior and its driving me nuts!

Any ideas?

Edgardo,

PS: I'm using VS 2005 Beta 2
 
E

Edgardo Rossetto

Ok, I found out what was my problem, the Page_Load events occurs BEFORE
the Title property is databinded (how stupid!), this is probably because
databinding is done using reflection, so the solution is to override the
OnPreRender event, and do the stuff there:

/*------------------------------------------------------------*/
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Literal1.Text = Title;
}
/*------------------------------------------------------------*/

And it works just fine.

/me embarrassed
 

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