20,53 €
The ASP.NET Data Presentation Controls are server controls to which you can bind data to organize and display it in different ways.
This book shows you how to get the most from these controls to easily display and manage complex data in your web applications. It takes you through each of the major data presentation controls in ASP.NET, gets you started using them, and then shows you how to use them in your applications for managing your data.
This book shows you how to get the most from the ASP.NET data presentation controls to easily display and manage complex data in your web applications
This book takes you through the most important tasks for working with the ASP.NET data presentation controls. Packed with example code and clear explanations, this book will make you a master of these controls in no time.
This book is for ASP.NET developers who want to display or manage data in ASP.NET applications. The code examples are in C#.
Sie lesen das E-Book in den Legimi-Apps auf:
Seitenzahl: 191
Veröffentlichungsjahr: 2007
Copyright © 2007 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, Packt Publishing, nor its dealers or distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: December 2007
Production Reference: 1141207
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-847193-95-7
www.packtpub.com
Cover Image by Karl Moore ( <[email protected]>)
Author
Joydip Kanjilal
Reviewers
Steven M. Swafford
Anand Narayanaswamy
Senior Acquisition Editor
Douglas Patterson
Development Editor
Rashmi Phadnis
Technical Editor
Bhupali Khule
Code Testing
Mithun Sehgal
Editorial Team Leader
Mithil Kulkarni
Project Manager
Abhijeet Deobhakta
Indexer
Hemangini Bari
Proofreader
Harminder Singh
Chris Smith
Cathy Cumberlidge
Angie Butcher
Production Coordinator
Shantanu Zagade
Cover Designer
Shantanu Zagade
Joydip Kanjilal is a Microsoft MVP in ASP.NET. He has over 12 years of industry experience in IT with more than 6 years in Microsoft .NET and its related technologies. He has authored a lot of articles for some of the most reputable sites like, www.asptoday.com, www.devx.com, www.aspalliance.com, www.aspnetpro.com, www.sswug.com, www.sql-server-performance.com, etc. A lot of these articles have been selected at www.asp.net —Microsoft's Official Site on ASP.NET. Joydip was also a community credit winner at www.community-credit.com a number of times.
He is currently working as a Senior Consultant in a reputable company in Hyderabad, INDIA. He has years of experience in designing and architecting solutions for various domains. His technical strengths include, C, C++, VC++, Java, C#, Microsoft .NET, Ajax, Design Patterns, SQL Server, Operating Systems and Computer Architecture. Joydip blogs at http://aspadvice.com/blogs/joydip and spends most of his time reading books, blogs and writing books and articles. His hobbies include watching cricket and soccer and playing chess.
Mail: <[email protected]>
I am grateful to my parents for their love, support, and inspiration throughout my life and would like to express my deepest respects to them. I am thankful to Piku, Indronil, and little Jini in particular for their co-operation, patience, and support. I am also thankful to the other members of my family for their continued encouragement and support.
I am thankful to Douglas Paterson and the entire PacktPub team for providing me the opportunity to author my first book. I am also thankful to Steve Smith and the entire AspAlliance team for providing me the opportunity to author my first ever article at AspAlliance. I would also like to thank the reviewers of this book for their invaluable feedback. I am thankful to Anand Narayanaswamy, Douglas Paterson, and Steven M. Swafford for their excellent suggestions, which I hope have helped a lot in improving the quality of the book. I am also thankful to Russell Jones of DevX and Jude Kelly of Sql-Server-Performance for their valuable technical advices. I am also thankful to Stephen Wynkoop of SSWUG and David Riggs of AspNetPro for giving me the opportunity to author articles there. I would also like to thank Abhishek Kant of Microsoft for the MVP award that I received in 2007.
I am thankful to my friends Sriram Putrevu, Rakesh Gujjar, and Tilak Tarafder and the readers of my articles for their invaluable feedback and suggestions. My special thanks to Balaji Desari, Ashish Agarwal, and Sanjay Golani for their inspiration and support.
Writing my first ever book has been a challenging, learning and a rewarding experience. It was really a nice time and I enjoyed it.
Steven M. Swafford began developing software in 1995 while serving in the United States Air Force (USAF). Upon leaving the USAF he continued developing leading-edge solutions in support of the America's war fighters as part of the original USAF enterprise portal development team. His roots are now in Auburn, Alabama where he works for Northrop Grumman Information Technology. Steven's credits his wife Su Ok and daughter Sarah for supporting and inspiring his ongoing passion for software development and the resultant challenges of life near the bleeding edge as well as his mother Pat Harris and father Cliff Swafford for believing in him. Steven would like to thank Tim Stewart and Edward Habal who were his professional mentors and to this day remain close friends as well as Frankie Elston, Joe Chaplin, and Glenn Regan all of whom are colleagues that Steven worked closely with for years.
This is Steven's second technical review. Steven previously worked as a technical editor on ODP.NET Developers Guide.
Mail: <[email protected]>
Website: http://www.radicaldevelopment.net
Blog: http://www.blog.radicaldevelopment.net
Anand Narayanaswamy works as an independent consultant and runs NetAns Hosting Services (www.netans.com), which provides web hosting services based in Trivandrum, Kerala State, India. Anand is a Microsoft Most Valuable Professional (MVP) in Visual C# (https://mvp.support.microsoft.com/profile/Anand) and is the author of Community Server Quickly (http://www.packtpub.com/community-server/book) published by Packt Publishing.
He works as the chief technical editor for ASPAlliance.com (http://aspalliance.com/author.aspx?uId=384030) and is also a member of ASPAlliance.com Advisory Board. He regularly contributes articles, and book and product reviews to ASPAlliance.com, C-Sharpcorner.com, Developer.com, Codeguru.com, Microsoft Academic Alliance and asp.netPRO magazine.
Anand has worked as a technical editor for several popular publishers such as Sams, Addison-Wesley Professional, Wrox, Deitel, and Manning. His technical editing skills helped the authors of Sams Teach Yourself the C# Language in 21 Days, Core C# and .NET, Professional ADO.NET 2, ASP.NET 2.0 Web Parts in Action and Internet and World Wide Web (4th Edition) to fine-tune the content. He has also contributed articles for Microsoft Knowledge Base and delivered podcast shows for Aspnetpodcast.com. He is a moderator for Windows MarketPlace Newsgroups.
Anand also runs LearnXpress.com (www.learnxpress.com), Dotnetalbum.com (www.dotnetalbum.com), CsharpFAQ.com (www.csharpfaq.com) and Devreviews.com (www.devreviews.com). LearnXpress.com is a featured site at MSDN's Visual C# .NET communities section. Anand has won several prizes at Community-Credit.com and has been featured as "All Time" contributor at the site. He is one of the founders of Trivandrum Microsoft Usergroup. He regularly blogs under the banner "I type what I feel" at http://msmvps.com/blogs/anandn.
Website: http://www.visualanand.net
Blog: http://weblogs.asp.net/anandn
When you design and implement an ASP.NET web application, you need to manage and display data to the end user in more than one way. Data Presentation Controls in ASP.NET are server controls to which you can bind data to organize and display it in different ways. This book covers the major data controls in ASP.NET (from ASP.NET 1.x to ASP.NET 3.5/Orcas). Packed with plenty of real-life code examples, tips, and notes, this book is a good resource for readers who want to display and manage complex data in web applications using ASP.NET by fully leveraging the awesome features that these data controls provide.
Chapter 1 discusses the ASP.NET data binding model and how we can work with the data source controls in ASP.NET.
Chapter 2 discusses how we can work with the various list controls in ASP.NET and illustrates how we can implement a custom control that extends the CheckBoxList control to provide added functionalities.
Chapter 3 discusses how we can display tables of data with the Repeater control. It also discusses how we can perform other operations, like paging and sorting data using this control.
Chapter 4 discusses how we can use the DataList control in ASP.NET. It also illustrates how we can bind images to the DataList control dynamically.
Chapter 5 discusses how we can display, edit, delete, and format data for customized display using the DataGrid control. It discusses how we can use this control for paging and sorting data. It also illustrates the implementation of a sample application using this control and how we can use this control to display data in a customized format.
Chapter 6 presents a discussion on the GridView control and performing various operation with it, like paging, sorting, inserting data, updating data, deleting data, and displaying data in customized format. It also discusses how one can implement a custom GridView control to display hierarchical data. It also discusses how one can export a GridView control to MS Excel and MS Word.
Chapter 7 explores the other view controls in ASP.NET, like DetailsView, FormView, and the TreeView control, and how we can use them to perform various operations.
Chapter 8 discusses LINQ, its features and benefits, and how it can be used to bind data to the new data controls in Orcas.
This book is for ASP.NET developers who want to display or manage data in ASP.NET applications. To use this book, you need to have access to ASP.NET and SQL Server.
The following is the list of software required for this book:
This book is for ASP.NET developers who want to display or manage data in ASP.NET applications. The code examples are in C#.
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
There are two styles for code. Code words in text are shown as follows: "You can also use the static method Eval() of the DataBinder class for binding data to your controls."
A block of code will be set as follows:
New terms and important words are introduced in a bold-type font. Words that you see on the screen, in menus or dialog boxes for example, appear in our text like this: "The second record is set to editable mode on clicking the Edit command button ".
Important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book, what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply drop an email to <[email protected]>>, making sure to mention the book title in the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or email <[email protected]>.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Visit http://www.packtpub.com/support, and select this book from the list of titles to download any example code or extra resources for this book. The files available for download will then be displayed.
The downloadable files contain instructions on how to use them.
Although we have taken every care to ensure the accuracy of our contents, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in text or code—we would be grateful if you would report this to us. By doing this you can save other readers from frustration, and help to improve subsequent versions of this book. If you find any errata, report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the Submit Errata link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata added to the list of existing errata. The existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
You can contact us at <[email protected]>if you are having a problem with some aspect of the book, and we will do our best to address it.
In ASP.NET, the term Data Binding implies binding the controls to data that is retrieved from the data source and hence providing a read or write connectivity between these controls and the data, that they are bound to. These data sources can be one of databases, xml files or even, flat files. We would use the word data controls often in this book to imply controls that can be bound to data from external data sources. In ASP.NET 1.x, you were introduced to a powerful data binding technique where you could eliminate the need of writing lengthy code that was used in earlier for binding data to data controls. With ASP.NET 2.0, you have a lot of new controls and features added in this context. You now have simplified paging, filtering, sorting, automatic updates, data source controls, and a host of other powerful features.
In this chapter, we will cover the following points:
In data binding, as we have discussed in the beginning, the controls are bound to data from the data source resulting in read or write connectivity between the controls and the data they are bound to. The controls are actually bound to the columns of the result set that contains the data. This result set can be a data set, a data table, a data reader, or any other instance of a collection type.
We need not write any code to display the control values after they are bound to these data sources. This kind of data binding allows you to bind data to the user interface controls without the need to write code.
In its simplest form, the syntax for using data binding in your ASPX pages is as follows:
Depending on whether you require binding single value data or a multiple or repeated value data to a control, you can have the following types of binding mechanisms in ASP.NET data controls:
Single value data binding, as the name suggests implies, binding of a single value or a single record, say, an employee's record. In contrast, repeated value data binding implies binding a set or a table of employee records.
You can use any of the following for single value data binding:
For repeated value data binding, you can use the following syntax:
The following sections presents a discussion on how you can use the data binding expressions in ASP.NET to bind data to the controls and a discussion on the newly added data source controls in ASP.NET 2.0 and their usage.
What are data binding expressions? Well, they are the code snippets that you use between the <%# and %> blocks in your ASP.NET web page. According to MSDN, "Data-binding expressions create bindings between any property on an ASP.NET page, including a server control property, and a data source when the DataBind() method is called on the page. You can include data-binding expressions on the value side of an attribute or value pair in the opening tag of a server control or anywhere in the page".
The following are the advantages of using Data Binding expressions in ASP.NET controls in the presentation layer:
You should use data binding in the ASP.NET web pages in the presentation layer of your application. The syntax used for data binding in ASP.NET 1.x is as follows:
The following code snippet illustrates how you can bind data to a label control using the syntax shown above:
You can also use the static method Eval() of the DataBinder class for binding data to your controls. This method has an overloaded version that accepts the format expression as an additional parameter that relates to the type of formatting that you would require on the data to be displayed. The syntax for using the Eval() method is shown as follows:
As shown in the code snippet the Eval() method accepts two parameters:
Refer to the following code snippet that illustrates, how you can use the DataBinder.Eval() method to bind data:
You can use the overloaded version of the Eval() method to specify the format expression (as an additional optional parameter) to display the data in your required format. Refer to the following code snippet:
This code would display the LoginDate in the label control named lblLoginDate as Sunday 15, April.
With ASP.NET 2.0, you have a much simpler syntax as the DataBinder instance is now the default context for all data binding expressions that are used for displaying non-hierarchical data in your presentation layer. You can now use any of the following overloaded versions of the Eval() method for binding data.
Before we dig into a discussion on the data source controls in ASP.NET that follows this section, I would present here two classes that we would frequently be using here and after in the book; I would use these classes throughout this book. In order to reduce code duplication or redundancy, I am providing here the source code for both these classes; we would refer them elsewhere.
The Employee class in this example is the Business Entity
