ASP.NET Tutorial/Data Binding/DataBound controls
Содержание
There are three main types of DataBound controls
<source lang="csharp">
list controls, tabular DataBound controls, and hierarchical DataBound controls. Working with List Controls ASP.NET 3.5 Framework includes the following five List controls: BulletedList: A bulleted list of items.
Each item can be displayed as text, a link button, or a hyperlink.
CheckBoxList: A list of check boxes.
Multiple check boxes in the list can be selected.
DropDownList: A drop-down list.
Only one item in the drop-down list can be selected.
ListBox: A list box.
only one item in the list can be selected or multiple items can be selected.
RadioButtonList: A list of radio buttons.
Only one radio button can be selected.
All five controls inherit from the same base ListControl class. <%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <body>
<form id="form1" runat="server">
BulletedList
<asp:BulletedList id="BulletedList1" DataSourceId="srcProducts" DataTextField="Title" Runat="server" />
CheckBoxList
<asp:CheckBoxList id="CheckBoxList1" DataSourceId="srcProducts" DataTextField="Title" Runat="server" />
DropDownList
<asp:DropDownList id="DropDownList1" DataSourceId="srcProducts" DataTextField="Title" Runat="server" />
ListBox
<asp:ListBox id="ListBox1" DataSourceId="srcProducts" DataTextField="Title" Runat="server" />
RadioButtonList
<asp:RadioButtonList id="RadioButtonList1" DataSourceId="srcProducts" DataTextField="Title" Runat="server" />
<asp:SqlDataSource id="srcProducts" ConnectionString="Data Source=.\SQLExpress; AttachDbFilename=|DataDirectory|MyDatabase.mdf; Integrated Security=True;User Instance=True" SelectCommand="SELECT Title FROM Products" Runat="server" /> </form>
</body> </html></source>
Use DetailsView and FormView to display a single data item at a time:
<source lang="csharp">
DetailsView: Displays a single data item in an HTML table.
This control enables you to display, page, edit, and add data.
FormView: Uses a template to display a single data item. Unlike the DetailsView, a FormView enables you to layout a form by using templates.
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <body>
<form id="form1" runat="server">
GridView
<asp:GridView id="GridView1" DataSourceId="srcProducts" Runat="server" />
DataList
<asp:DataList id="DataList1" DataSourceId="srcProducts" RepeatColumns="2" Runat="server"> <ItemTemplate> <%#Eval("Title")%> directed by <%#Eval("Director")%> </ItemTemplate> </asp:DataList>
DetailsView
<asp:DetailsView id="DetailsView1" DataSourceId="srcProducts" AllowPaging="true" Runat="server" />
FormView
<asp:FormView id="FormView1" DataSourceId="srcProducts" AllowPaging="true" Runat="server"> <ItemTemplate> <%#Eval("Title")%> directed by <%#Eval("Director")%> </ItemTemplate> </asp:FormView>
Repeater
<asp:Repeater id="Repeater1" DataSourceId="srcProducts" Runat="server"> <ItemTemplate> <%#Eval("Title")%> directed by <%#Eval("Director")%> </ItemTemplate> </asp:Repeater>
ListView
<asp:ListView id="ListView1" DataSourceId="srcProducts" Runat="server"> <LayoutTemplate>
<asp:DataPager ID="pager1" PageSize="3" runat="server"> <Fields> <asp:NumericPagerField /> </Fields> </asp:DataPager> </LayoutTemplate> <ItemTemplate> <%#Eval("Title")%> directed by <%#Eval("Director")%> </ItemTemplate> </asp:ListView>
<asp:SqlDataSource id="srcProducts" ConnectionString="Data Source=.\SQLExpress; AttachDbFilename=|DataDirectory|MyDatabase.mdf; Integrated Security=True;User Instance=True" SelectCommand="SELECT TOP 5 Title,Director FROM Products" Runat="server" /> </form>
</body> </html></source>
Working with Tabular DataBound Controls
<source lang="csharp">
There are six tabular DataBound controls. These controls can be divided into two types: those that display multiple data items at a time and those that display a single data item at a time. First, you can use any of the following controls to display a set of data items: GridView: A set of data items in an HTML table.
This control enables you to display, sort, page, select, and edit data.
DataList: A set of data items in an HTML table.
Unlike the GridView control, more than one data item can be displayed in a single row.
Repeater: Displays a set of data items using a template.
Repeater control does not automatically render an HTML table.
ListView: Displays a set of data items using a template.
ListView control supports sorting, paging, and editing database data.</source>