telliot79
asked on
asp.net bind repeater control from code behind
experts,
I'm making a call to my sql database and attempting to display the results in a repeater control in labels. I want to manipulate the data before I populate the labels. I realize I need to do this in the _ItemDataBound method, but can't figure out how to tie my labels to the data. so far, I have this:
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Label lblCode = (Label)e.Item.FindControl( "lblCode") ;
}
how do I bind the code from sql select statement to my label?
Thanks in advance
I'm making a call to my sql database and attempting to display the results in a repeater control in labels. I want to manipulate the data before I populate the labels. I realize I need to do this in the _ItemDataBound method, but can't figure out how to tie my labels to the data. so far, I have this:
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Label lblCode = (Label)e.Item.FindControl(
}
how do I bind the code from sql select statement to my label?
Thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
okay, fixed that, it was looking at the repeater header. I wrapped the code in the following:
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingIt em)
and now am making progress. I'm now receiving this error:
Unable to cast object of type 'System.Data.Common.DataRe cordIntern al' to type 'System.Data.DataRowView'.
i'll continue working away
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingIt
and now am making progress. I'm now receiving this error:
Unable to cast object of type 'System.Data.Common.DataRe
i'll continue working away
I guess it depends on what is used as data source (I used a DataTable), and the properties of the repeater. If you want I can share the minimal code I used but I guess it would be more valuable to work with your code, can you share more of it?
ASKER
clearly I'm just figuring this out as I go, so thanks for your patience. I'm using a SqlDataReader:
using (SqlDataReader rdr = cmd.ExecuteReader())
{
try
{
rpt.DataSource = rdr;
rpt.DataBind();
}
does that help?
using (SqlDataReader rdr = cmd.ExecuteReader())
{
try
{
rpt.DataSource = rdr;
rpt.DataBind();
}
does that help?
yeah that probably means the type of e.Item.DataItem will be different. I'll see if I can reproduce it. Alternatively you could use a SqlDataAdapter to fill a DataTable and set that as the DataSource but I'm not sure if that would have other effects on your project.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Legend...
thanks for the help
thanks for the help
ASKER
I'm getting the following error: object reference not set to an instance of an object.
from this line of code: string strCode = ((DataRowView)(e.Item.Data
caused by this:
((DataRowView)(e.Item.Data
'((System.Data.DataRowView
any ideas?