Using PagedDataSource with a Repeater

J

jazzart

Hi there,

I'm fairly new to programming with Asp.Net 2.0 so I'm finding myself
regularly fumbling around in the dark a bit, so to speak.

I'm currently using Visual Web Developer and have been following the
www.asp.net data access tutorials to create a DAL and BLL to connect
my web site (a simple real estate property search web site) to my
database (Sql Express 2005).

For my search page I'm using a Repeater to display the search results,
which is bound to an ObjectDataSource that gets the data using a
SelectMethod in my custom BLL class (which calls a stored procedure,
passing through the select parameters). Things have been going fairly
smoothly until I needed to implement paging with the repeater. That in
itself is not the problem as there was a tutorial for that. My problem
is this:

I've created an event handler method for the ItemDataBound event on my
Repeater. With each item (and alternating item) I manipulate certain
fields from my data source for customised display. It was all working
fine until I implemented PagedDataSource - which changes the
datasource returned to the ObjectDataSource from my custom
PropertiesDataTable to PagedDataSource. This is the event handler:

protected void rptPropertyList_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
// Programmatically reference the PropertiesRow instance
bound to this RepeaterItem
MyDataClass.PropertiesRow property =
(MyDataClass.PropertiesRow)
((System.Data.DataRowView)e.Item.DataItem).Row;

Label AgentBranchLabel =
(Label)e.Item.FindControl("lblAgentBranch");

if (property.AgentBranchCode.Trim().ToLower() == "jamh")
{
AgentBranchLabel.Text = "custom output";
}
else if (property.AgentBranchCode.Trim().ToLower() ==
"jamw")
{
AgentBranchLabel.Text = "other custom output";
}
}
}

Now this returns the error:
---------------------------
Unable to cast object of type 'PropertiesRow' to type
'System.Data.DataRowView'.
Exception Details: System.InvalidCastException: Unable to cast object
of type 'PropertiesRow' to type 'System.Data.DataRowView'.

Stack Trace:
[InvalidCastException: Unable to cast object of type 'PropertiesRow'
to type 'System.Data.DataRowView'.]
search.rptPropertyList_ItemDataBound(Object sender,
RepeaterItemEventArgs e) in c:\Work\jamesmillard\search.aspx.cs:69

System.Web.UI.WebControls.Repeater.OnItemDataBound(RepeaterItemEventArgs
e) +105
System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex,
ListItemType itemType, Boolean dataBind, Object dataItem) +142
System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean
useDataSource) +454
System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +53
System.Web.UI.WebControls.Repeater.DataBind() +72
System.Web.UI.WebControls.Repeater.EnsureDataBound() +55
System.Web.UI.WebControls.Repeater.OnPreRender(EventArgs e) +12
System.Web.UI.Control.PreRenderRecursiveInternal() +77
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+1360
---------------------------

I'm not sure what I need to do in order to access these fields for
each row in my ItemDataBound event handler. I'm a bit lost at the
moment, and I can't seem to find any article that talks about this, so
any help would be greatly appreciated!!!

Cheers,
Karen
 
J

jazzart

Hi there,

I'm fairly new to programming with Asp.Net 2.0 so I'm finding myself
regularly fumbling around in the dark a bit, so to speak.

I'm currently using Visual Web Developer and have been following thewww.asp.netdata access tutorials to create a DAL and BLL to connect
my web site (a simple real estate property search web site) to my
database (Sql Express 2005).

For my search page I'm using a Repeater to display the search results,
which is bound to an ObjectDataSource that gets the data using a
SelectMethod in my custom BLL class (which calls a stored procedure,
passing through the select parameters). Things have been going fairly
smoothly until I needed to implement paging with the repeater. That in
itself is not the problem as there was a tutorial for that. My problem
is this:

I've created an event handler method for the ItemDataBound event on my
Repeater. With each item (and alternating item) I manipulate certain
fields from my data source for customised display. It was all working
fine until I implemented PagedDataSource - which changes the
datasource returned to the ObjectDataSource from my custom
PropertiesDataTable to PagedDataSource. This is the event handler:

protected void rptPropertyList_ItemDataBound(object sender,
RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
// Programmatically reference the PropertiesRow instance
bound to this RepeaterItem
MyDataClass.PropertiesRow property =
(MyDataClass.PropertiesRow)
((System.Data.DataRowView)e.Item.DataItem).Row;

Label AgentBranchLabel =
(Label)e.Item.FindControl("lblAgentBranch");

if (property.AgentBranchCode.Trim().ToLower() == "jamh")
{
AgentBranchLabel.Text = "custom output";
}
else if (property.AgentBranchCode.Trim().ToLower() ==
"jamw")
{
AgentBranchLabel.Text = "other custom output";
}
}

}

Now this returns the error:
---------------------------
Unable to cast object of type 'PropertiesRow' to type
'System.Data.DataRowView'.
Exception Details: System.InvalidCastException: Unable to cast object
of type 'PropertiesRow' to type 'System.Data.DataRowView'.

Stack Trace:
[InvalidCastException: Unable to cast object of type 'PropertiesRow'
to type 'System.Data.DataRowView'.]
search.rptPropertyList_ItemDataBound(Object sender,
RepeaterItemEventArgs e) in c:\Work\jamesmillard\search.aspx.cs:69

System.Web.UI.WebControls.Repeater.OnItemDataBound(RepeaterItemEventArgs
e) +105
System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex,
ListItemType itemType, Boolean dataBind, Object dataItem) +142
System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean
useDataSource) +454
System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +53
System.Web.UI.WebControls.Repeater.DataBind() +72
System.Web.UI.WebControls.Repeater.EnsureDataBound() +55
System.Web.UI.WebControls.Repeater.OnPreRender(EventArgs e) +12
System.Web.UI.Control.PreRenderRecursiveInternal() +77
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Control.PreRenderRecursiveInternal() +161
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
+1360
---------------------------

I'm not sure what I need to do in order to access these fields for
each row in my ItemDataBound event handler. I'm a bit lost at the
moment, and I can't seem to find any article that talks about this, so
any help would be greatly appreciated!!!

Cheers,
Karen

Don't worry I've found the solution to my problem. :)

I changed:
MyDataClass.PropertiesRow property = (MyDataClass.PropertiesRow)
((System.Data.DataRowView)e.Item.DataItem).Row;

To:
MyDataClass.PropertiesRow property =
(MyDataClass.PropertiesRow)e.Item.DataItem;
 

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

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top