ASP.NET Tutorial/Data Binding/DataBound controls — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 11:59, 26 мая 2010

There are three main types of DataBound controls

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">
    <div class="floater">
    <h3>BulletedList</h3>
    <asp:BulletedList
        id="BulletedList1"
        DataSourceId="srcProducts"
        DataTextField="Title"
        Runat="server" />
    </div>
    <div class="floater">
    <h3>CheckBoxList</h3>
    <asp:CheckBoxList
        id="CheckBoxList1"
        DataSourceId="srcProducts"
        DataTextField="Title"
        Runat="server" />
    </div>
    <div class="floater">
    <h3>DropDownList</h3>
    <asp:DropDownList
        id="DropDownList1"
        DataSourceId="srcProducts"
        DataTextField="Title"
        Runat="server" />
    </div>
    <div class="floater">
    <h3>ListBox</h3>
    <asp:ListBox
        id="ListBox1"
        DataSourceId="srcProducts"
        DataTextField="Title"
        Runat="server" />
    </div>
    <div class="floater">
    <h3>RadioButtonList</h3>
    <asp:RadioButtonList
        id="RadioButtonList1"
        DataSourceId="srcProducts"
        DataTextField="Title"
        Runat="server" />
    </div>
    <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>


Use DetailsView and FormView to display a single data item at a time:

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">
    <div class="floater">
    <h3>GridView</h3>
    <asp:GridView
        id="GridView1"
        DataSourceId="srcProducts"
        Runat="server" />
    </div>

    <div class="floater">
    <h3>DataList</h3>
    <asp:DataList
        id="DataList1"
        DataSourceId="srcProducts"
        RepeatColumns="2"
        Runat="server">
        <ItemTemplate>
        <%#Eval("Title")%>
        <i>directed by</i>
        <%#Eval("Director")%>
        </ItemTemplate>
    </asp:DataList>
    </div>

    <div class="floater">
    <h3>DetailsView</h3>
    <asp:DetailsView
        id="DetailsView1"
        DataSourceId="srcProducts"
        AllowPaging="true"
        Runat="server" />
    </div>
    <div class="floater">
    <h3>FormView</h3>
    <asp:FormView
        id="FormView1"
        DataSourceId="srcProducts"
        AllowPaging="true"
        Runat="server">
        <ItemTemplate>
        <%#Eval("Title")%>
        <i>directed by</i>
        <%#Eval("Director")%>
        </ItemTemplate>
    </asp:FormView>
    </div>
    <br style="clear:both" />
    <div class="floater">
    <h3>Repeater</h3>
    <asp:Repeater
        id="Repeater1"
        DataSourceId="srcProducts"
        Runat="server">
        <ItemTemplate>
        <%#Eval("Title")%>
        <i>directed by</i>
        <%#Eval("Director")%>
        </ItemTemplate>
    </asp:Repeater>
    </div>
    <div class="floater">
    <h3>ListView</h3>
    <asp:ListView
        id="ListView1"
        DataSourceId="srcProducts"
        Runat="server">
        <LayoutTemplate>
        <div id="itemPlaceholder" runat="server">
        </div>
        <asp:DataPager ID="pager1" PageSize="3" runat="server">
        <Fields>
            <asp:NumericPagerField />
        </Fields>
        </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
        <%#Eval("Title")%>
        <i>directed by</i>
        <%#Eval("Director")%>
        </ItemTemplate>
    </asp:ListView>
    </div>
    <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>


Working with Tabular DataBound Controls

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.