<%@ Import Namespace="System.Data" %>
<%@ Page Language = "VB" %>
 
<html>
<title>第十七例 DataGrid、DataList及Repeater控件的使用</title>
   <script runat="server">
 
    Function CreateDataSource() As ICollection
        Dim dt As New DataTable()
        Dim dr As DataRow
        
        dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
        dt.Columns.Add(New DataColumn("PriceValue", GetType(String)))
        dt.Columns.Add(New DataColumn("DescriptionValue", GetType(String)))
        
        Dim i As Integer
        For i = 0 To 5
            dr = dt.NewRow()
            dr(0) = "Item " & i.ToString()
            dr(1) =(1.23 *(i + 1)).ToString()
            dr(2) = "Item " + i.ToString() + "的描述"
            dt.Rows.Add(dr)
        Next i
        
        Dim dv As New DataView(dt)
        Return dv
    End Function '构造数据源函数CreateDataSource

    Sub Change1(sender As Object, e As EventArgs)
	If DropDownList1.SelectedIndex = 0 Then
	    DataGrid1.DataSource = CreateDataSource()
            DataGrid1.DataBind()
	    DataList1.Visible=false
	    Repeater1.Visible=false
	    DataGrid1.Visible=true
	Else If DropDownList1.SelectedIndex = 1 Then
            DataList1.DataSource = CreateDataSource()
            DataList1.DataBind()
	    DataGrid1.Visible=false
	    Repeater1.Visible=false
	    DataList1.Visible=true
	Else
            Repeater1.DataSource = CreateDataSource()
            Repeater1.DataBind()
	    DataGrid1.Visible=false
	    DataList1.Visible=false
	    Repeater1.Visible=true
	End If
    End Sub 'End of Change1

    Sub Page_Load(sender As Object, e As EventArgs)
        If Not IsPostBack Then
            DataList1.DataSource = CreateDataSource()
            DataList1.DataBind()
	    DataGrid1.Visible=false
	    Repeater1.Visible=false
	    DataList1.Visible=true
        End If
    End Sub '在初始化页面时用DataList显示数据
     
   </script>
 
<body>
 
   <form runat=server>

      <h3><font face="Verdana">第十七例 DataGrid、DataList及Repeater控件的使用</font></h3>
 
      <asp:DataGrid id="DataGrid1" runat="server" BorderColor="black" BorderWidth="1" CellPadding="3" AutoGenerateColumns="true" runat="server">

         <HeaderStyle BackColor="#00aaaa">
         </HeaderStyle>
	 <AlternatingItemStyle BackColor="Gainsboro">
         </AlternatingItemStyle>

      </asp:DataGrid><!-- DataGrid的AuotGenerateColumns属性默认设定为True，不需要自定义其各元素的模板等内容 -->	

      <asp:DataList id="DataList1" runat="server" BorderColor="black" CellPadding="3" Font-Name="Verdana" Font-Size="8pt" ExtractTemplateRows="true" GridLines="Both">

         <HeaderStyle BackColor="#aaaadd">
         </HeaderStyle>
         <AlternatingItemStyle BackColor="Gainsboro">
         </AlternatingItemStyle>

         <HeaderTemplate>
            <asp:Table id="Table1" runat="server">
                <asp:TableRow>
                   <asp:TableCell
                        ColumnSpan="2">
                      DataList
                   </asp:TableCell>
                </asp:TableRow>
            </asp:Table>
         </HeaderTemplate>               

         <ItemTemplate>
            <asp:Table id="Table2" runat="server">
                <asp:TableRow>
                   <asp:TableCell
                        Text='<%# DataBinder.Eval(Container.DataItem, "StringValue") %>'>
                   </asp:TableCell>
                   <asp:TableCell
                        Text='<%# DataBinder.Eval(Container.DataItem, "PriceValue") %>'>
                   </asp:TableCell>
                </asp:TableRow>
                <asp:TableRow>
                   <asp:TableCell
                        ColumnSpan="2"
                        Text='<%# DataBinder.Eval(Container.DataItem, "DescriptionValue") %>'>
                   </asp:TableCell>
                </asp:TableRow>
            </asp:Table>
         </ItemTemplate>
 
      </asp:DataList>
      <!-- ExtractTemplateRows设定为True可以根据需要在DataList的每个单元中再定义Table -->

      <asp:Repeater id="Repeater1" runat="server">

          <HeaderTemplate>
             <font size=5><b>Repeater:</b></font><br>
          </HeaderTemplate>
             
          <ItemTemplate>
             <%# DataBinder.Eval(Container.DataItem, "StringValue") %>的值为：<%# DataBinder.Eval(Container.DataItem, "PriceValue") %>
          </ItemTemplate>
             
          <SeparatorTemplate>，</SeparatorTemplate>
      </asp:Repeater>
      <!-- Repeater控件的灵活使用，可以任意使用数据源数据根据需要来显示（不一定为表格形式） -->
      <br>
      <b>选择使用哪种控件显示数据：</b>
      <asp:DropDownList id="DropDownList1" runat="server"
	AutoPostBack=True OnSelectedIndexChanged="Change1">
         <asp:ListItem>DataGrid控件显示</asp:ListItem>
         <asp:ListItem Selected>DataList控件显示</asp:ListItem>
         <asp:ListItem>Repeater控件显示</asp:ListItem>
      </asp:DropDownList> （默认为DataList）
   </form>
 
</body>
</html>