 |
| ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 13th, 2009, 02:48 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
ItemDataBound on ListView
Hello,
I'm trying to convert an example of a RowDataBound event for a GridView, to an ItemDataBound event for a ListView.
This is what I'm starting with:
protected void gridProducts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string prodtype = (string)DataBinder.Eval(e.Row.DataItem, "ProductType");
if (prodtype == "Sample" )
etc.
This is as far as I get on the conversion:
protected void listProducts_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
string prodtype = (string).....
Where do I go next?
Thanks,
David.
|
|

June 13th, 2009, 03:12 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
|
|
Cast to ListViewDataItem
Hi David,
In the ItemDataBound event for items which are ListViewItemType.DataItem, e.Item is of type ListViewDataItem. You can cast e.Item to this and then use its DataItem property the same as with the GridView. The example on msdn at http://msdn.microsoft.com/en-us/libr...wdataitem.aspx is similar.
So your code could be:
Code:
protected void listProducts_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
string prodtype = (string)DataBinder.Eval(dataItem, "ProductType");
// ...
}
}
HTH
Phil
|
|

June 13th, 2009, 04:57 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
|
|
Sorry Phillip, there is a slight error in your code.
Code:
protected void listProducts_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataItem = (ListViewDataItem) e.Item;
string prodtype = (string) DataBinder.Eval(dataItem.DataItem, "ProductType");
if (prodtype == "Sample")
{
// do your thing here
}
}
}
You got the right idea though.
|
|
The Following 2 Users Say Thank You to Lee Dumond For This Useful Post:
|
|
|

June 14th, 2009, 02:35 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Thanks for that. It seems that I overestimated my own abilities to continue from there!
What I want to do is change the value in one column depending on the value in another, as the row is being built.
I thought I could use:
string prodtype = (string) DataBinder.Eval(dataItem.DataItem, "ProductType");
if (prodtype == "Sample")
{
(string) DataBinder.Eval(dataItem.DataItem, "ProductText") = "Sample product";
}
where ProductType and ProductText are the 2 columns.
But the page wont run with this code.
|
|

June 14th, 2009, 04:24 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
|
|
Two reasons for the problem you're having:
1. The Eval method just evaluates the databinding method. It does not allow you to change the value.
2. The ItemDataBound event is too late to change any of the values anyway, because they are already bound when this event fires.
In order to do this, you need to handle the ItemCreated event. Then, you need to modify the DataRowView that is being used to display the item.
Code:
protected void listProducts_ItemCreated(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataItem = (ListViewDataItem) e.Item;
string prodtype = (string) DataBinder.Eval(dataItem.DataItem, "ProductType");
DataRowView view = (DataRowView)dataItem.DataItem;
if (prodtype == "Sample")
{
view["ProductText"] = "Sample product";
}
}
}
|
|

June 15th, 2009, 12:10 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
|
|
Sorry Lee, there is a slight error in your code.
OK I had to dig deep to find it, but it's worth mentioning that ItemCreated is raised each time the layout of the ListView is created, not just when databinding happens. For example if you do a postback later on, ItemCreated will be raised but DataItem won't have a value, so when you try to set view["ProductText"] it will raise a NullReferenceException.
So you just need to add a null check to your ItemCreated event:
Code:
protected void listProducts_ItemCreated(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
ListViewDataItem dataItem = (ListViewDataItem) e.Item;
if(dataItem.DataItem != null)
{
string prodtype = (string) DataBinder.Eval(dataItem.DataItem, "ProductType");
DataRowView view = (DataRowView)dataItem.DataItem;
if (prodtype == "Sample")
{
view["ProductText"] = "Sample product";
}
}
}
}
Phil
|
|

June 15th, 2009, 12:26 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
|
|
Phil,
You sure about that? Remember, the ListView is going to be re-bound on postbacks.
I just tried this on my end (Northwind database) with a button to postback the page, and encountered no problems.
|
|

June 15th, 2009, 01:05 PM
|
|
Friend of Wrox
|
|
Join Date: Sep 2005
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
|
|
Indeed, depending on how it is bound. But remember ItemCreated is also raised before Page_Load on each request, so update events etc can be raised, at which point it will not be binding.
It works in this case because DataBinder.Eval returns null, and so if(prodtype=="Sample") returns false.
If you change it to something like this, you will get an exception:
Code:
if (prodtype == "Sample" || prodtype == null)
{
view["ProductText"] = "Sample product";
}
|
|

June 15th, 2009, 01:45 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
|
|
Sorry Phil, I'm not following you.
DataItem is not going to be null as long as the item type is ListViewItemType.DataItem, and we are checking for that.
And if the item type is ListViewItemType.DataItem, prodtype will never be null as long as there is data in that field.
Not sure where you are getting the "ItemCreated is also raised before Page_Load on each request" thing. Certainly not on MSDN.
|
|

June 15th, 2009, 03:06 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Phil, Lee,
Thanks for your help.
Dave.
|
|
 |