ASP.NET Data Presentation Controls Essentials - Joydip Kanjilal - E-Book

ASP.NET Data Presentation Controls Essentials E-Book

Joydip Kanjilal

0,0
20,53 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

In Detail

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

Approach

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.

Who this book is for

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:

Android
iOS
von Legimi
zertifizierten E-Readern

Seitenzahl: 191

Veröffentlichungsjahr: 2007

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

ASP.NET Data Presentation Controls Essentials
Credits
About the Author
Acknowledgements
About the Reviewers
Preface
What This Book Covers
What You Need for This Book
Who is This Book for
Conventions
Reader Feedback
Customer Support
Downloading the Example Code for the Book
Errata
Questions
1. Introduction to Data Binding in ASP.NET
The ASP.NET Data Binding Model
Using the Data Binding Expressions
The Employee and the Data Manager Classes
New Data Source Controls in ASP.NET 2.0
The Object Data Source Control
Object Data Source Control Methods
Using the Object Data Source Control
The SQL Data Source Control
Using the SQL Data Source Control
The Access Data Source Control
Using the Access Data Source Control
The XML Data Source Control
Using the XML Data Source Control
User Interface and Data Source Paging
User Interface and Data Source Sorting
Filtering Data Using the Object Data Source Control
Summary
2. Working with List Controls in ASP.NET
The ASP.NET List Controls
Working with the ListBox Control
Appending List Items to the ListBox Control
Selecting One or More List Items
Removing List Items from the ListBox Control
Binding Data to the ListBox Control
Handling ListBox Control Events
Working with the DropDown List Control
Appending List Items to the DropDownList Control
Selecting a List Item
Removing List Items from the DropDownList Control
Binding Data to the DropDownList Control
Handling DropDownList Control Events
Associating Event Handlers to a dynamically generated DropDownList Control
Implementing a Simple Application
Working with the CheckBoxList Control
Appending List Items to the CheckBoxList Control
Selecting One or More List Items
Removing List Items from the CheckBoxList Control
Binding Data to the CheckBoxList Control
Handling CheckBoxList Control Events
Implementing a CustomCheckBoxList Control
Working with the BulletedList Control
Appending List Items to the BulletedList Control
Selecting a List Item
Removing List Items from the BulletedList Control
Binding Data to the BulletedList Control
Handling BulletedList Control Events
Working with the RadioButtonList Control
Appending List Items to the RadioButtonList Control
Selecting a List Item
Removing List Items from the RadioButtonList Control
Binding Data to the RadioButtonList Control
Handling RadioButtonList Control Events
Summary
3. Working with the Repeater Control
The ASP.NET Repeater Control
Using the Repeater Control
Displaying Data Using the Repeater Control
Displaying Checkboxes in a Repeater Control
Implementing Data Paging Using the Repeater Control
The BindPagedData() Method
Navigating through the Pages
Sorting Data Using the Repeater Control
Revisiting the DataManager Class
Filtering Data Using the Repeater Control
Handling Repeater Control Events
Summary
4. Working with the DataList Control
The ASP.NET DataList Control
Using the DataList Control
Displaying Data
Handling Events
Binding Images Dynamically
Selecting Data
Editing data
Deleting Data
Summary
5. Working with the DataGrid Control in ASP.NET
The ASP.NET DataGrid Control
Creating a DataGrid Control
Implementing a Sample Application Using DataGrid Control
Displaying Data
Styling the DataGrid Control
Appending Data Using the DataGrid Control
Editing Data Using the DataGrid Control
Deleting Data Using the DataGrid Control
Paging Using the DataGrid Control
Summary
6. Displaying Views of Data (Part I)
The ASP.NET GridView Control
Comparing DataGrid and GridView Controls
Displaying DropDownList in a GridView Control
Displaying CheckBox in a GridView Control
Change the Row Color of GridView Control Using JavaScript
Displaying Tool Tip in a GridView Control
Paging Using the GridView Control
Implementing a Hierarchical GridView
Sorting Data Using the GridView Control
Inserting, Updating and Deleting Data Using the GridView Control
Exporting the GridView Data
Formatting the GridView Control
Summary
7. Displaying Views of Data (Part II)
Working with the ASP.NET DetailsView Control
Using the DetailsView Control
Working with the ASP.NET FormView Control
Formatting Data Using the FormView Control
Working with the ASP.NET TreeView Control
Implementing a Directory Structure as a TreeView
Summary
8. Working with LINQ
Introducing LINQ
Why LINQ?
Understanding the LINQ Architecture
Operators in LINQ
Querying Data Using LINQ
The New Data Controls in VS.NET 2008 (Orcas)
Using the ListView Control
Using the DataPager Control
Data Binding Using LINQ
Summary
Index

ASP.NET Data Presentation Controls Essentials

Joydip Kanjilal

ASP.NET Data Presentation Controls Essentials

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]>)

Credits

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

About the Author

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]>

Acknowledgements

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.

About the Reviewers

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

Preface

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.

What This Book Covers

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.

What You Need for This Book

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:

ASP.NET 2.0 (For Chapters 1 - 7)ASP.NET 3.5 (Orcas) (For Chapter 8)SQL Server 2005

Who is This Book for

This book is for ASP.NET developers who want to display or manage data in ASP.NET applications. The code examples are in C#.

Conventions

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:

<asp:ListBox ID="ListBox1" runat="server" Height="125px" Width="214px"> <asp:ListItem Value="1">Joydip</asp:ListItem> <asp:ListItem Value="2">Douglas</asp:ListItem> <asp:ListItem Value="3">Jini</asp:ListItem> <asp:ListItem Value="4">Piku</asp:ListItem> <asp:ListItem Value="5">Rama</asp:ListItem> <asp:ListItem Value="6">Amal</asp:ListItem> <asp:ListItem Value="7">Indronil</asp:ListItem> </asp:ListBox>

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 ".

Note

Important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader Feedback

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.

Customer Support

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.

Downloading the Example Code for the Book

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.

Errata

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.

Questions

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.

Chapter 1. Introduction to Data Binding in ASP.NET

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:

The ASP.NET Data Binding ModelData Binding ExpressionsThe ASP.NET Data Source Controls
Object Data Source ControlSQL Data Source ControlAccess Data Source ControlXML Data Source Control

The ASP.NET Data Binding Model

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.

Note

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:

<%# Data Source Name %>

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 BindingRepeated Value Data Binding

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:

<%# Name of the Property %> <%# Expression %> <%# Method Name, Parameter List %>

For repeated value data binding, you can use the following syntax:

<%# Name of the Data Source %>

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.

Using the Data Binding Expressions

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:

Flexibility to use any data binding expressions provided that the value it resolves to is one that the data control can use.You can use these expressions to bind any property to its corresponding data.Flexibility to bind one property to one data source and another property to another data source.

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:

<%# Container.DataItem("expression") %>

The following code snippet illustrates how you can bind data to a label control using the syntax shown above:

<asp:Label id="lblUserName" runat="server" Text='<%# Container.DataItem("UserName") %>'> </asp:Label>

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:

<%# DataBinder.Eval(Container.DataItem, "expression"[, "format"]) %>

As shown in the code snippet the Eval() method accepts two parameters:

The first of these parameters is the data container, that is, a data table, a data set or a data view.The second parameter is a reference to the value that needs to be assigned to the control.

Refer to the following code snippet that illustrates, how you can use the DataBinder.Eval() method to bind data:

<asp:Label id="lblUserName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "UserName") %>'> </asp:Label>

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:

<asp:Label id="lblLoginDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "LoginDate", "{0:dddd d MMMM"]) %>'> </asp:Label>

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.

<%# Eval("expression") %> <%# Eval("expression"[, "format"]) %>

The Employee and the Data Manager Classes

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