一、整理兩個的aspx檔
(1). GridView
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="自己的檔名.aspx.cs" Inherits="自己的類別" %> <!DOCTYPE html> <html> <head runat="server"> <meta http-equiv="Content-Type" content="text/html"; charset="utf-8"/> <title>Template</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="BID" HeaderText="BID"/> <asp:TemplateField HeaderText="Button"> <ItemStyle Width="50%"/> <ItemTemplate> <asp:Button id="btn" runat="server" Text="test" OnCommand="btn_OnClick" CommandArgument='<%# Eval("BID")%>' Enabled='<%# Eval("Record").ToString()=="Y"? false:true %>'/> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
(1). Repeter
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="自己的檔案.cs"Inherits="自己的類別" %> <!DOCTYPE html> <html> <head runat="server"> <meta http-equiv="Content-Type" content="text/html"; charset="utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater ID="Repeater1" runat="server" > <HeaderTemplate> <table border="1" cellpadding="0" cellspacing="0" style="width:1006px;border-collapse:collapse; text-align:center;"> <tr> <th style="background-color:red">ID</th> <th style="background-color:red">內容</th> </tr> </HeaderTemplate> <ItemTemplate> <tr> <td><asp:Label ID="lblId" runat="server" Text='<%# Eval("id") %>' ></asp:Label></td> <td><%# Eval("name") %></td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </div> </form> </body> </html>
以上整理,是供平常練習的快速樣本!
(3). CodeBehide 資料來源的寫法,以GridView舉例
//將後端的DataTable丟到前端,記得dt要先add,然後用迴圈塞資料,GridView才能顯示 DataTable dt =new DataTable(); (this.可加可不加)GridView1.DataSource =dt; (this.可加可不加)GridView1.DataBind();
最後謝謝這篇大大的分享,讓我學習到怎麼在網誌中將程式碼插入網誌中!