综合

img abandonship

DataBind包括三大方法,Repeater,DataList和DataGrid,这些控件都位于 System.Web.UI.WebControls 命名空间中,从 WebControl 基类中直接或间接派生出来的。这些方法都是通过HTML来显示数据的内

发表于2004/10/30 20:57:00  2543人阅读

分类: ASP.NET

        DataBind包括三大方法,Repeater,DataList和DataGrid,这些控件都位于 System.Web.UI.WebControls 命名空间中,从 WebControl 基类中直接或间接派生出来的。这些方法都是通过HTML来显示数据的内容。

一、DataList
          Repeater 控件是通用的迭代程序,而 DataList 控件则提供专门用于控制列表布局的附加功能。与 Repeater 不同,DataList 呈现其模
板定义元素周围的表行和单元格,从而提供了更为丰富的布局和格式设置功能。例如,DataList 支持 RepeatColumns 和 RepeatDirection 属性,这两项属性分别指定列数和数据项呈现方向(垂直或水平)。DataList 还支持样式特性,如字体大小和字体名称。以下示例分别用了vb.net和c#两种语言来显示如何访问一个包含书名及其他信息的 SQL 数据库,并使用 DataList 控件来显示相关数据。

[VB.net]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="VB" runat="server">
   Sub Page_Load(sender As Object, e As EventArgs)
      Dim myConnection As SqlConnection
      Dim myCommand As SqlDataAdapter
      ' Create a connection to the "pubs" SQL database located on the
      ' local computer.
         myConnection = New SqlConnection("server=localhost;" _
            & "database=pubs;Trusted_Connection=Yes")
      ' Connect to the SQL database using a SQL SELECT query to get all
      ' the data from the "Titles" table.
      myCommand = New SqlDataAdapter("SELECT * FROM Titles", myConnection)
      ' Create and fill a DataSet.
      Dim ds As DataSet = new DataSet()
      myCommand.Fill(ds)
      ' Bind MyDataList to the DataSet. MyDataList is the ID for
      ' the DataList control in the HTML section of the page.
      MyDataList.DataSource = ds
      MyDataList.DataBind()
   End Sub
</script>

<%--  Display the data. -%>
<body>
      <%-- Open the DataList control and set it for two columns, to be
      filled in horizontal order. --%>
      <ASP:DataList id="MyDataList" RepeatColumns="2"
         RepeatDirection="Horizontal" runat="server">
      <ItemTemplate>
      <div style="padding:15,15,15,15;font-size:10pt;font-family:Verdana">
      <div style="font:12pt verdana;color:darkred">
         <i><b><%# DataBinder.Eval(Container.DataItem, "title")%>
         </i></b>
      </div>
      <br>
      <b>Title ID: </b>
      <%# DataBinder.Eval(Container.DataItem, "title_id") %><br>
      <b>Category: </b>
      <%# DataBinder.Eval(Container.DataItem, "type")%><br>
      <b>Publisher ID: </b>
      <%# DataBinder.Eval(Container.DataItem, "pub_id") %><br>
      <b>Price: </b>
      <%# DataBinder.Eval(Container.DataItem, "price", "{0:c}") %>
      <p>
      </div>
      </ItemTemplate>
   </ASP:DataList>
</body>
</html>


[C#]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
   void Page_Load(Object sender, EventArgs e)
   {
      // Create a connection to the "pubs" SQL database located on the
      // local computer.
      SqlConnection myConnection = new SqlConnection("server=localhost;" +
         "database=pubs;Trusted_Connection=Yes");
      // Connect to the SQL database using a SQL SELECT query to get all
      // the data from the "Titles" table.
      SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * " +
         " from Titles", myConnection);
      // Create and fill a DataSet.
      DataSet ds = new DataSet();
      myCommand.Fill(ds);
      // Bind MyDataList to the DataSet. MyDataList is the ID for
      // the DataList control in the HTML section of the page.
      MyDataList.DataSource = ds;
      MyDataList.DataBind();
   }
</script>

<%-- Display the data. -%>
<body>
   <%-- Open the DataList control and set it for two columns, to be  
      filled in horizontal order. --%>
   <ASP:DataList id="MyDataList" RepeatColumns="2"
      RepeatDirection= "Horizontal" runat="server">
      <%-- Create a DataList control template named "ItemTemplate". --%>
      <ItemTemplate>
         <div style="padding:15,15,15,15;font-size:10pt;
            font-family:Verdana">
            <div style="font:12pt verdana;color:darkred">
               <i><b><%# DataBinder.Eval(Container.DataItem, "title")%>
               </i></b>
            </div>
            <br>
            <b>Title ID: </b>
            <%# DataBinder.Eval(Container.DataItem, "title_id") %><br>
            <b>Category: </b>
            <%# DataBinder.Eval(Container.DataItem, "type")%><br>
            <b>Publisher ID: </b>
            <%# DataBinder.Eval(Container.DataItem, "pub_id") %><br>
            <b>Price: </b>
            <%# DataBinder.Eval(Container.DataItem,"price", "{0:c}") %>
            <p>
         </div>
      </ItemTemplate>
   </ASP:DataList>
</body>
</html>

二、Repeater

        Repeater 控件是一种数据绑定列表控件,其外观完全由其模板来控制。与 DataList 不同,Repeater 控件不在 HTML 表中呈现其模板,并且不具有对选择或编辑的内置支持。以下代码示例显示一个绑定到 SqlDataReader 的 Repeater 控件,该控件返回从一个 SQL 查询中返回的一组只读、只进数据记录,该 SQL 查询包含有关一套书籍的信息。SqlDataReader 在该示例中用于实现最高性能。该示例还定义一个 HeaderTemplate 和一个 FooterTemplate,它们分别在列表的开头和结尾呈现。Repeater 控件为 DataSource 集合中的每一项呈现一次 ItemTemplate,从而迭代绑定数据。它只呈现其模板中包含的元素。

[VB.net]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
   <script language="VB" runat="server">
      Sub Page_Load(sender As Object, e As EventArgs)
         '  Create a connection to the "pubs" SQL database located
         ' on the local computer.
         Dim myConnection As SqlConnection
         Dim myCommand As SqlDataAdapter
         ' Connect to the SQL database using a SQL SELECT query to get
         ' all the data from the "Titles" table.
         myConnection = New SqlConnection("server=localhost;" _
            & "database=pubs;Trusted_Connection=Yes")
         myCommand = New SqlDataAdapter("SELECT * FROM Titles", _
            myConnection)
         ' Create and fill a  DataSet.
         Dim ds As Dataset = new DataSet()
         myCommand.Fill(ds)
         ' Bind MyRepeater to the  DataSet. MyRepeater is the ID of the
         ' Repeater control in the HTML section of the page.
         MyRepeater.DataSource = ds
         MyRepeater.DataBind()
      End SUb
   </script>

<body>
   <ASP:Repeater id="MyRepeater" runat="server">
      <HeaderTemplate>
         <Table width="100%" style="font: 8pt verdana">
            <tr style="background-color:DFA894">
               <th>
                  Title
               </th>
               <th>
                  Title ID
               </th>
               <th>
                  Type
               </th>
               <th>
                  Publisher ID
               </th>
               <th>
                  Price
               </th>
            </tr>
      </HeaderTemplate>
      <ItemTemplate>
            <tr style="background-color:FFECD8">
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "title") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "title_id") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "type") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "pub_id") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "price", _
                     "{0:c}") %>
               </td>
            </tr>
      </ItemTemplate>
      <FooterTemplate>
         </table>
      </FooterTemplate>
   </ASP:Repeater>
</body>
</html>

[C#]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
   void Page_Load(Object sender, EventArgs e)
   {
      //  Create a connection to the "pubs" database located
      // on the local computer.
      SqlConnection myConnection = new SqlConnection("server=localhost;" +
         "database=pubs;Trusted_Connection=Yes");
      // Connect to the SQL database using a SQL SELECT query to get
      // all the data from the "Titles" table.
      SqlDataAdapter myCommand = new SqlDataAdapter("SELECT * FROM" +
         " Titles", myConnection);
      // Create and fill a DataSet.
      DataSet ds = new DataSet();
      myCommand.Fill(ds);
      // Bind MyRepeater to the  DataSet. MyRepeater is the ID of the
      // Repeater control in the HTML section of the page.
      MyRepeater.DataSource = ds;
      MyRepeater.DataBind();
   }
</script>

<%--  Display the data in the body of the page. --%>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0">
   <ASP:Repeater id="MyRepeater" runat="server">
      <HeaderTemplate>
         <Table width="100%" style="font: 8pt verdana">
            <tr style="background-color:DFA894">
               <th>
                  Title
               </th>
               <th>
                  Title ID
               </th>
               <th>
                  Type
               </th>
               <th>
                  Publisher ID
               </th>
               <th>
                  Price
               </th>
            </tr>
      </HeaderTemplate>

      <ItemTemplate>
            <tr style="background-color:FFECD8">
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "title") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem,"title_id") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "type") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem, "pub_id") %>
               </td>
               <td>
                  <%# DataBinder.Eval(Container.DataItem,
                     "price", "{0:c}") %>
               </td>
            </tr>
      </ItemTemplate>

      <FooterTemplate>
         </Table>
      </FooterTemplate>
   </ASP:Repeater>
</body>
</html>

三、DataGrid

         多功能的 DataGrid 服务器控件显示表格数据并支持对数据进行选择、排序和编辑。默认情况下,DataGrid 将为数据源中的每个字段生成一个绑定列 (AutoGenerateColumns=true)。每个数据字段都按照它们在数据库的存储顺序显示在单独的列中。以下示例显示作者姓名、地址、电话号码以及其他数据的列表。

[Visual Basic]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="VB" runat="server">
   Sub Page_Load(Src As Object, e As EventArgs)
      Dim myConnection As SqlConnection
      Dim myCommand As SqlDataAdapter
      ' Create a connection to the "pubs" SQL database located on the
      ' local computer.
      myConnection = New SqlConnection("server=localhost;" _
         & "database=pubs;Trusted_Connection=Yes")
      ' Connect to the SQL database using a SQL SELECT query to get all
      ' the data from the "Authors" table.
      myCommand = new SqlDataAdapter("SELECT * FROM Authors", _
         myConnection)
      ' Create and fill a DataSet.
      Dim ds As DataSet = new DataSet()
      myCommand.Fill(ds)
      ' Bind MyDataGrid to the DataSet. MyDataGrid is the
      ' ID for the DataGrid control in the HTML section.
      MyDataGrid.DataSource = ds
      MyDataGrid.DataBind()
   End Sub
</script>

<body>
   <h3><font face="Verdana">
      Simple Select to a DataGrid Control.
   </font></h3>
   <ASP:DataGrid id="MyDataGrid" runat="server"
      Width="700"
      BackColor="#ccccff"
      BorderColor="black"
      ShowFooter="false"
      CellPadding=3
      CellSpacing="0"
      Font-Name="Verdana"
      Font-Size="8pt"
      HeaderStyle-BackColor="#aaaadd"
      EnableViewState="false"
   />
</body>
</html>

[C#]
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<script language="C#" runat="server">
   protected void Page_Load(Object Src, EventArgs E)
   {
      // Create a connection to the "pubs" SQL database located on the
      // local computer.
      SqlConnection myConnection = new SqlConnection("server=localhost;" +
         "database=pubs;Trusted_Connection=Yes");
      // Connect to the SQL database using a SQL SELECT query to get all
      // the data from the "Authors" table.
      SqlDataAdapter myCommand = new SqlDataAdapter("SELECT " +
         " * FROM Authors", myConnection);
      // Create and fill a DataSet.
      DataSet ds = new DataSet();
      myCommand.Fill(ds);
      // Bind MyDataGrid to the DataSet. MyDataGrid is the ID for the
      // DataGrid control in the HTML section.
      DataView source = new DataView(ds.Tables[0]);
      MyDataGrid.DataSource = source ;
      MyDataGrid.DataBind();
   }
</script>

<body>
   <%-- Display the DataGrid information in the  body of the page. --%>
   <h3><font face="Verdana">Simple SELECT to a DataGrid Control
   </font></h3>
   <%-- Display the data in a DataGrid. --%>
   <ASP:DataGrid id="MyDataGrid" runat="server"
      Width="700"
      BackColor="#ccccff"
      BorderColor="black"
      ShowFooter="false"
      CellPadding=3
      CellSpacing="0"
      Font-Name="Verdana"
      Font-Size="8pt"
      HeaderStyle-BackColor="#aaaadd"
      EnableViewState="false"
   />
</body>
</html>

阅读全文
0 0

相关文章推荐

img
取 消
img