資料繫結技術—如何使用DataBinder.Eval()方法進行資料繫結

來源:互聯網
上載者:User

<%@ 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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.