Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion 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
 
Old June 24th, 2012, 04:34 PM
Authorized User
 
Join Date: Sep 2011
Posts: 41
Thanks: 3
Thanked 4 Times in 3 Posts
Default Listview, general question

Hi,

I am wondering on the flexibility of using a listview control. E.g., Can I add code to check a value within a list item, and then decide whether to change the background color of the list item box? Would this code go into an Item event? I would have guessed the ItemCreating event except that this event doesn't exist. Maybe this custom code can go in the ItemCreated event?

I was using a user control prior because it gave me great ease and flexibility on layout. And I could simply put code into the code behind. However, since everyone is telling me that I should use a listview control, I would like to know if it is just as easy to custom code the layout design with a listview.

Thanks,

- nano
 
Old June 25th, 2012, 03:03 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Well, I suggested a ListView because it seems a lot easier than generating user controls on the fly and manage your own state. That's not to say that you have to use the ListView and that a UC is a bad practice; it all depends on your design and what you're trying to accomplish.

That said, ItemCreated is indeed a good place. Here's a quick example:

Code:
protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
{
  Label description = e.Item.FindControl("DescriptionLabel") as Label;
  Picture picture = e.Item.DataItem as Picture;
  if (picture != null)
  {
    if (picture.Id  > 5)
    {
      description.CssClass = "Alert";
    }
  }
}
I am using FindControl to find a control inside the ItemTemplate. I then cast DataItem to the item I am binding to the control; a Picture in my case. When the picture is not null I am changing the appearance of the Label by assigning a CssClass. In this example, I am only doing that when the Id is > 5, but clearly this is just an example and you can apply any business logic you see fit.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old June 25th, 2012, 05:11 AM
AnnieCalvert
Guest
 
Posts: n/a
Default

The method of data presentation depends on the number of headers in the grid. Upon display of each row the grid searches for a header for it, and if it doesn't find it, a header from the previous hierarchy level is used. This enables to implement the grid and tree list view in a single component.
http://www.dapfor.com/en/net-suite/net-grid/tutorial
 
Old June 25th, 2012, 05:17 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

How does this help the OP? THis is a WinForms grid....

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old June 25th, 2012, 06:08 AM
Authorized User
 
Join Date: Sep 2011
Posts: 41
Thanks: 3
Thanked 4 Times in 3 Posts
Default Thanks Imar !

Thanks for the answer/insight.

I really appreciate the code sample as it provides a great guide ... I'll def attempt to use this in the future ...

- nano





Similar Threads
Thread Thread Starter Forum Replies Last Post
General Information Question seananderson ASP.NET 1.0 and 1.1 Basics 4 February 15th, 2007 12:55 PM
General SELECT question rsearing SQL Server 2005 1 January 16th, 2007 12:01 PM
General Question About c++ madeebmir C++ Programming 0 August 15th, 2006 10:48 AM
Very general question merianos VB How-To 4 August 1st, 2006 12:24 PM
General Question stu9820 ASP.NET 1.0 and 1.1 Basics 2 September 23rd, 2003 03:36 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.