ASP.Net/Data Binding/Repeater — различия между версиями

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

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

ASP:Repeater Data binding

<%@ Page Language=VB Debug=true %>
<%@ Import Namespace="System.Data" %>
<script runat=server>
Private MyDT As New DataTable
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
    Dim MyRow As DataRow
    MyDT.Columns.Add(New DataColumn("EmployeeID", _
        GetType(Int32)))
    MyDT.Columns.Add(New DataColumn("EmployeeFirstName", _
        GetType(String)))
    MyDT.Columns.Add(New DataColumn("EmployeeLastName", _
        GetType(String)))
    MyDT.Columns.Add(New DataColumn("BirthDate", _
        GetType(Date)))
    MyDT.Columns.Add(New DataColumn("Salary", _
        GetType(Single)))
    MyRow = MyDT.NewRow()
    MyRow(0) = 1
    MyRow(1) = "Bob"
    MyRow(2) = "Miller"
    MyRow(3) = "5/15/65"
    MyRow(4) = "40000"
    MyDT.Rows.Add(MyRow)
    MyRow = MyDT.NewRow()
    MyRow(0) = 2
    MyRow(1) = "Jenny"
    MyRow(2) = "Fry"
    MyRow(3) = "7/22/75"
    MyRow(4) = "73050"
    MyDT.Rows.Add(MyRow)
    MyRow = MyDT.NewRow()
    MyRow(0) = 3
    MyRow(1) = "Lisa"
    MyRow(2) = "Smith"
    MyRow(3) = "12/8/71"
    MyRow(4) = "62500"
    MyDT.Rows.Add(MyRow)
    MyRow = MyDT.NewRow()
    MyRow(0) = 4
    MyRow(1) = "Stephanie"
    MyRow(2) = "Myer"
    MyRow(3) = "3/15/54"
    MyRow(4) = "43222"
    MyDT.Rows.Add(MyRow)
    MyRow = MyDT.NewRow()
    MyRow(0) = 5
    MyRow(1) = "Jimmy"
    MyRow(2) = "Parker"
    MyRow(3) = "3/12/47"
    MyRow(4) = "52825"
    MyDT.Rows.Add(MyRow)
    rpt1.DataSource = MyDT
    rpt1.DataBind()
End Sub
</SCRIPT>
<HTML>
<HEAD>
<TITLE>Repeater Control Sample Page</TITLE>
</HEAD>
<BODY  >
<form runat="server">
<Font Face="Tahoma">
<ASP:Repeater 
    id="rpt1"
    runat="server"
    >
    <HeaderTemplate>
        <Table width="100%" style="font: 8pt verdana">
        <TR style="Background-Color:DFECD8">
        <TD><B>Employee ID</B></TD>
        <TD><B>First Name</B></TD>
        <TD><B>Last Name</B></TD>
        <TD><B>Birth Date</B></TD>
        <TD><B>Salary</B></TD>
        </TR>
    </HeaderTemplate>
    <ItemTemplate>
        <TR style="Background-Color:FFECD9">
        <TD>
            <%# DataBinder.Eval(Container.DataItem, "EmployeeID") %>
        </TD>
        <TD>
            <%# DataBinder.Eval(Container.DataItem, "EmployeeFirstName") %>
        </TD>
        <TD>
            <%# DataBinder.Eval(Container.DataItem, "EmployeeLastName") %>
        </TD>
        <TD>
            <%# Format(DataBinder.Eval(Container.DataItem, "BirthDate"), "Short Date") %>
        </TD>
        <TD>
            <%# FormatCurrency(DataBinder.Eval(Container.DataItem, "Salary")) %>
        </TD>
        </TR>
    </ItemTemplate>
    <AlternatingItemTemplate>
        <TR style="Background-Color:FFECA8">
        <TD>
            <%# DataBinder.Eval(Container.DataItem, "EmployeeID") %>
        </TD>
        <TD>
            <%# DataBinder.Eval(Container.DataItem, "EmployeeFirstName") %>
        </TD>
        <TD>
            <%# DataBinder.Eval(Container.DataItem, "EmployeeLastName") %>
        </TD>
        <TD>
            <%# Format(DataBinder.Eval(Container.DataItem, "BirthDate"), "Short Date") %>
        </TD>
        <TD>
            <%# FormatCurrency(DataBinder.Eval(Container.DataItem, "Salary")) %>
        </TD>
        </TR>
    </AlternatingItemTemplate>
    <SeparatorTemplate>
        <TR style="Background-Color:White">
        <TD>&nbsp</TD>
        <TD>&nbsp</TD>
        <TD>&nbsp</TD>
        <TD>&nbsp</TD>
        <TD>&nbsp</TD>
        </TR>
    </SeparatorTemplate>
    <FooterTemplate>
        <TR style="Background-Color:DFECD8">
        <TD><B>Employee ID</B></TD>
        <TD><B>First Name</B></TD>
        <TD><B>Last Name</B></TD>
        <TD><B>Birth Date</B></TD>
        <TD><B>Salary</B></TD>
        </TR>
        </Table>
    </FooterTemplate>
</ASP:Repeater>

</Font>
</Form>
</BODY>
</HTML>



Bind data to asp:repeater

<%@ Page Language="vb" %>
<%@ Import Namespace="System.Data" %>
<html>
<head>
   <title>Databound Control Example</title>
   <script runat="server">
      Sub Page_Load()
         MyDataGrid.DataSource = CreateData()
         MyDataGrid.DataBind()
         MyDataList.DataSource = CreateData()
         MyDataList.DataBind()
         MyRepeater.DataSource = CreateData()
         MyRepeater.DataBind()
      End Sub
      Function CreateData() As DataTable
         Dim DT As New DataTable()
         Dim Row1, Row2, Row3, Row4 As DataRow
         DT.Columns.Add(New DataColumn("name", System.Type.GetType("System.String")))
         DT.Columns.Add(New DataColumn("city", System.Type.GetType("System.String")))
         Row1 = DT.NewRow()
         Row1("name") = "Name 1"
         Row1("city") = "City 1"
         DT.Rows.Add(Row1)
         Row2 = DT.NewRow()
         Row2("name") = "Name 2"
         Row2("city") = "City 2"
         DT.Rows.Add(Row2)
         Row3 = DT.NewRow()
         Row3("name") = "Name 3"
         Row3("city") = "City 3"
         DT.Rows.Add(Row3)
         Row4 = DT.NewRow()
         Row4("name") = "Name 4"
         Row4("city") = "City 4"
         DT.Rows.Add(Row4)
         Return DT
      End Function
   </script>
</head>
<body>
   <h1>Databound Control Example</h1>
   <form runat="server">
      <asp:table id="MyTable" border="1" cellpadding="5" cellspacing="0" runat="server">
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               DataGrid Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:datagrid id="MyDataGrid"
                  allowpaging="true"
                  allowsorting="true"
                  alternatingitemstyle-backcolor="LightSkyBlue"
                  backcolor="Blue"
                  forecolor="White"
                  cellpadding="2"
                  cellspacing="0"
                  headerstyle-backcolor="DarkBlue"
                  headerstyle-forecolor="Yellow"
                  pagerstyle-mode="NumericPages"
                  pagesize="5"
                  runat="server"/>
            </asp:tablecell>
         </asp:tablerow>
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               DataList Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:datalist id="MyDataList"
                  alternatingitemstyle-backcolor="LightSkyBlue"
                  backcolor="Blue"
                  bordercolor="Black"
                  cellpadding="2"
                  cellspacing="0"
                  forecolor="White"
                  headerstyle-backcolor="DarkBlue"
                  headerstyle-forecolor="Yellow"
                  repeatcolumns="1"
                  repeatdirection="vertical"
                  repeatlayout="table" 
                  runat="server">
                  <headertemplate>
                     Composers
                  </headertemplate>
                  <itemtemplate>
                     <%# databinder.eval(container.dataitem, "name") %>
                  </itemtemplate>
               </asp:datalist>
            </asp:tablecell>
         </asp:tablerow>
         <asp:tablerow runat="server">
            <asp:tablecell runat="server">
               Repeater Control:
            </asp:tablecell>
            <asp:tablecell runat="server">
               <asp:repeater id="MyRepeater" runat="server">
                  <headertemplate>
                     <table cellpadding="5" cellspacing="0">
                        <tr>
                           <td>Name<hr/></td>
                           <td>City<hr/></td>
                        </tr>
                  </headertemplate>
                  <itemtemplate>
                        <tr>
                           <td><%# DataBinder.Eval(Container.DataItem, _
                                  "name") %></td>
                           <td><%# DataBinder.Eval(Container.DataItem, _
                                  "city") %></td>
                        </tr>
                  </itemtemplate>
                  <footertemplate>
                     </table>
                  </footertemplate>
               </asp:repeater>
            </asp:tablecell>
         </asp:tablerow>
      </asp:table>
   </form>
</body>
</html>