<%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.SqlClient" %> <html> <script language="C#" runat="server"> void Page_Load(Object semder, EventArgs e) { // 創建數據庫連接字符串及SqlDataAdapter對象 string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer_pubs"]; SqlConnection myConnection = new SqlConnection(ConnStr); SqlDataAdapter myCommand = new SqlDataAdapter("select * from Titles", myConnection); // 生成DataSet對象並填充數據 DataSet ds = new DataSet(); myCommand.Fill(ds, "Titles"); // 將Repeater控件進行數據綁定 MyRepeater.DataSource = ds.Tables["Titles"].DefaultView; MyRepeater.DataBind(); } </script> <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}") %> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </ASP:Repeater> </body> </html>
出處:http://mjgforever.cnblogs.com/