使用DataBinder.Eval()方法进行数据绑定


<%@ 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>
« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3