Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 13th, 2009, 02:48 PM
Authorized User
 
Join Date: Mar 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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.
  #2 (permalink)  
Old June 13th, 2009, 03:12 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default 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
  #3 (permalink)  
Old June 13th, 2009, 04:57 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

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.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following 2 Users Say Thank You to Lee Dumond For This Useful Post:
philip_cole (June 14th, 2009)
  #4 (permalink)  
Old June 14th, 2009, 02:35 PM
Authorized User
 
Join Date: Mar 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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.
  #5 (permalink)  
Old June 14th, 2009, 04:24 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

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";
      }
   }
}
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #6 (permalink)  
Old June 15th, 2009, 12:10 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

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
  #7 (permalink)  
Old June 15th, 2009, 12:26 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

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.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #8 (permalink)  
Old June 15th, 2009, 01:05 PM
Friend of Wrox
Points: 894, Level: 11
Points: 894, Level: 11 Points: 894, Level: 11 Points: 894, Level: 11
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: London, , United Kingdom.
Posts: 166
Thanks: 2
Thanked 33 Times in 33 Posts
Default

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";
}
  #9 (permalink)  
Old June 15th, 2009, 01:45 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

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.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #10 (permalink)  
Old June 15th, 2009, 03:06 PM
Authorized User
 
Join Date: Mar 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Phil, Lee,

Thanks for your help.

Dave.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid ItemDataBound Event Problem Grahame2003 C# 1 December 3rd, 2011 03:10 AM
ItemDataBound to Grid Within Repeater rstelma ASP.NET 2.0 Professional 4 July 9th, 2008 02:45 AM
Confirm in ItemDataBound rstelma ASP.NET 1.0 and 1.1 Basics 3 December 6th, 2006 02:10 AM
Datagrid ItemDataBound column widht eresina ASP.NET 1.0 and 1.1 Basics 0 August 10th, 2005 04:42 AM
DataGrid ItemDataBound Wyatt70 ADO.NET 2 November 22nd, 2004 03:31 PM



All times are GMT -4. The time now is 11:18 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.