Discussion:
ItemDataBound not Firing on Repeater
Brady Kelly
2007-09-06 16:38:47 UTC
Permalink
I have a user control that comprises a Repeater that builds a vertical table
of buttons. In the Page_Load event of the user control I bind an event
handler, but the event never fires. My code is like this:



protected void Page_Load(object sender, EventArgs e)

{

rptPageButtons.ItemDataBound += new
RepeaterItemEventHandler(rptPageButtons_ItemDataBound);

}



void rptPageButtons_ItemDataBound(object sender,
RepeaterItemEventArgs e)

{

throw new Exception("The method or operation is not
implemented.");

}



However, if I use the OnItemDataBound attribute, the event fires. Why could
this be?


===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Efran Cobisi
2007-09-07 07:30:49 UTC
Permalink
Hi Brady,

You didn't show us where and when you are calling the DataBind() method.
If this happens before the Page_Load() method is called then the
ItemDataBound event handler cannot be called because it hasn't been
attached at that time yet. Also, can you check if the page has the
AutoEventWireup property set to true? If not, the Page_Load() method
would not be attached to the Load event for the page thus the
ItemDataBound event handler would never be attached for this page.

Hope this helps.

--
Efran Cobisi
http://www.cobisi.com
Post by Brady Kelly
I have a user control that comprises a Repeater that builds a vertical table
of buttons. In the Page_Load event of the user control I bind an event
protected void Page_Load(object sender, EventArgs e)
{
rptPageButtons.ItemDataBound += new
RepeaterItemEventHandler(rptPageButtons_ItemDataBound);
}
void rptPageButtons_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
throw new Exception("The method or operation is not
implemented.");
}
However, if I use the OnItemDataBound attribute, the event fires. Why could
this be?
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Brady Kelly
2007-09-07 13:21:33 UTC
Permalink
Thanks Efran,

The Page_Load event does fire, assigning the handler to the event without
complaint. The event just doesn't fire when I call DataBind later.


public DataTable DataSource
{
set
{
pagingManager.MaxPageSize = 15;
pagingManager.PaginateDataTable(value);
rptPageButtons.DataSource = pagingManager.PagesDataTable;
}
}

public new void DataBind()
{
rptPageButtons.DataBind();
}
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: 07 September 2007 09:31 AM
Subject: Re: [DOTNET-WEB] ItemDataBound not Firing on Repeater
Hi Brady,
You didn't show us where and when you are calling the DataBind() method.
If this happens before the Page_Load() method is called then the
ItemDataBound event handler cannot be called because it hasn't been
attached at that time yet. Also, can you check if the page has the
AutoEventWireup property set to true? If not, the Page_Load() method
would not be attached to the Load event for the page thus the
ItemDataBound event handler would never be attached for this page.
Hope this helps.
--
Efran Cobisi
http://www.cobisi.com
Post by Brady Kelly
I have a user control that comprises a Repeater that builds a
vertical table
Post by Brady Kelly
of buttons. In the Page_Load event of the user control I bind an
event
Post by Brady Kelly
protected void Page_Load(object sender, EventArgs e)
{
rptPageButtons.ItemDataBound += new
RepeaterItemEventHandler(rptPageButtons_ItemDataBound);
}
void rptPageButtons_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
throw new Exception("The method or operation is not
implemented.");
}
However, if I use the OnItemDataBound attribute, the event fires.
Why could
Post by Brady Kelly
this be?
===================================
This list is hosted by DevelopMentorR http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentorR http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
brian zinn
2007-09-07 14:29:35 UTC
Permalink
Brady,

Some of your code is missing, but is it possible that you are calling
the databind when the page is post back? In that case your events
won't be firing. Sorry if that's too obvious, but you're not showing
where you do your binding.

Cheers,
Brian
Post by Brady Kelly
Thanks Efran,
The Page_Load event does fire, assigning the handler to the event without
complaint. The event just doesn't fire when I call DataBind later.
public DataTable DataSource
{
set
{
pagingManager.MaxPageSize = 15;
pagingManager.PaginateDataTable(value);
rptPageButtons.DataSource = pagingManager.PagesDataTable;
}
}
public new void DataBind()
{
rptPageButtons.DataBind();
}
-----Original Message-----
From: Discussion of building .NET applications targeted for the Web
Sent: 07 September 2007 09:31 AM
Subject: Re: [DOTNET-WEB] ItemDataBound not Firing on Repeater
Hi Brady,
You didn't show us where and when you are calling the DataBind() method.
If this happens before the Page_Load() method is called then the
ItemDataBound event handler cannot be called because it hasn't been
attached at that time yet. Also, can you check if the page has the
AutoEventWireup property set to true? If not, the Page_Load() method
would not be attached to the Load event for the page thus the
ItemDataBound event handler would never be attached for this page.
Hope this helps.
--
Efran Cobisi
http://www.cobisi.com
Post by Brady Kelly
I have a user control that comprises a Repeater that builds a
vertical table
Post by Brady Kelly
of buttons. In the Page_Load event of the user control I bind an
event
Post by Brady Kelly
protected void Page_Load(object sender, EventArgs e)
{
rptPageButtons.ItemDataBound += new
RepeaterItemEventHandler(rptPageButtons_ItemDataBound);
}
void rptPageButtons_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
throw new Exception("The method or operation is not
implemented.");
}
However, if I use the OnItemDataBound attribute, the event fires.
Why could
Post by Brady Kelly
this be?
===================================
This list is hosted by DevelopMentorR http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentorR http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentor(r) http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Brady Kelly
2007-09-07 16:27:43 UTC
Permalink
This is my code in the page hosting my control. The pgnTest.DataSource and
pgnTest.DataBind calls refer to the methods described below, from my
previous post:

protected void Page_Load(object sender, EventArgs e)
{
// pgnTest is a Paginator control on this test page.
DataTable emps = SqlDataProvider.GetEmployees();
pgnTest.DataSource = emps;
pgnTest.DataBind();
}


public partial class Paginator : System.Web.UI.UserControl
{
PagingManager pagingManager = new PagingManager();
protected void Page_Load(object sender, EventArgs e)
{
// This doesn't hook up the event, while using the attribute
on the Repeater does.
rptPageButtons.ItemDataBound += new
RepeaterItemEventHandler(rptPageButtons_ItemDataBound);
}

protected void rptPageButtons_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
Button btnPage = e.Item.FindControl("pageButton") as Button;
DataRowView drv = e.Item.DataItem as DataRowView;
if (drv != null)
{
btnPage.Attributes.Add("PageKey",
drv["PageKey"].ToString());
btnPage.Text = drv["PageDesc"].ToString();
}
}

public DataTable DataSource
{
set
{
pagingManager.MaxPageSize = 15;
pagingManager.PaginateDataTable(value);
rptPageButtons.DataSource = pagingManager.PagesDataTable;
}
}

public new void DataBind()
{
rptPageButtons.DataBind();
}
}

===================================
This list is hosted by DevelopMentor� http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Efran Cobisi, cobisi.com
2007-09-09 14:04:51 UTC
Permalink
Hi Brady,

Your issue is basically due to the fact that you are wiring up the
ItemDataBound event of the inner repeater AFTER it has been data bound.
The Load event for controls hosted inside a given Page instance are
fired AFTER the Load event has been fired for the aforementioned Page
instance. So, since you are data binding the control inside the Load
event for the page, it will be data bound before the Load event for the
control is fired.
To solve this one, just move the event wire up code in an event handler
wich would take place before the Load event. For example, the Init event
would be ok; here's some code:

protected override void OnInit(EventArgs e)
{
base.OnInit(e);

// Events wire up

rptPageButtons.ItemDataBound += new
RepeaterItemEventHandler(rptPageButtons_ItemDataBound);
}

Hope this helps.

--
Efran Cobisi
http://www.cobisi.com
Post by Brady Kelly
This is my code in the page hosting my control. The pgnTest.DataSource and
pgnTest.DataBind calls refer to the methods described below, from my
protected void Page_Load(object sender, EventArgs e)
{
// pgnTest is a Paginator control on this test page.
DataTable emps = SqlDataProvider.GetEmployees();
pgnTest.DataSource = emps;
pgnTest.DataBind();
}
public partial class Paginator : System.Web.UI.UserControl
{
PagingManager pagingManager = new PagingManager();
protected void Page_Load(object sender, EventArgs e)
{
// This doesn't hook up the event, while using the attribute
on the Repeater does.
rptPageButtons.ItemDataBound += new
RepeaterItemEventHandler(rptPageButtons_ItemDataBound);
}
protected void rptPageButtons_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
Button btnPage = e.Item.FindControl("pageButton") as Button;
DataRowView drv = e.Item.DataItem as DataRowView;
if (drv != null)
{
btnPage.Attributes.Add("PageKey",
drv["PageKey"].ToString());
btnPage.Text = drv["PageDesc"].ToString();
}
}
public DataTable DataSource
{
set
{
pagingManager.MaxPageSize = 15;
pagingManager.PaginateDataTable(value);
rptPageButtons.DataSource = pagingManager.PagesDataTable;
}
}
public new void DataBind()
{
rptPageButtons.DataBind();
}
}
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com
===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com
Loading...