標籤:
Repeater與GridView等資料列表一樣,都是用來顯示資料庫的資訊的,其中Repeater是最基本的列表形式,其用法也比較靈活。
一、Repeater的位置:
工具箱-資料-Repeater
拖拽進入頁面後的顯示:
切換到源視圖會發現他只有兩句代碼:
<asp:Repeater ID="Repeater1" runat="server">
</asp:Repeater>
其他的格式等需要我們用代碼來實現。
二、Repeater基本欄表格式:
<asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate><!--頭部列表的標籤--> <table> <tr> <td>地名</td> <td>郵編</td> <td>管理</td> <td>刪除</td> </tr> </HeaderTemplate> <ItemTemplate><!--表主題內容標籤—> <tr> <td></td> <td></td> <td></td> <td></td> </tr> </ItemTemplate> <FooterTemplate><!--表的腳部標籤--> </table> </FooterTemplate> </asp:Repeater>
表頭需要寫入<HeaderTemplate>..</HeaderTemplate>標籤中,即表的第一行放在此標籤中。
主要資料放在<ItemTemplate>..</ItemTemplate>標籤中,即表的主要資料內容的顯示,只需要一行即可,綁定資料後系統會自動產生其他的行。
表的尾部<FooterTemplate>..</FooterTemplate>標籤,尾部註解等。
三、Repeater的資料繫結。
需要先在HTML的頁面中指定綁定的表的列名
(還需要建立LINQ to SQL的類,來串連資料庫,附資料庫表圖,只需要類似的表即可)
使用<%#Eval("列名") %>,格式如下(這裡只綁定了兩個列):
<ItemTemplate> <tr> <td><%#Eval("Name") %></td> <td><%#Eval("PostCode") %></td> <td></td> <td></td> </tr> </ItemTemplate>
成功綁定後在設計檢視中的顯示:
cs中的代碼:
private DiquDataContext diqu;//外部定義LINQ方便使用 protected void Page_Load(object sender, EventArgs e) { diqu = new DiquDataContext();//初始化LINQ if (!IsPostBack)//第一次載入時 { BindData(); } } //建立了一個資料繫結的方法以方便使用 public void BindData() { //按條件查詢資料 var que = from m in diqu.Members where m.ParentId.ToString().Length == 2 select m;//按字元個數確定省級地區 //綁定資料 Repeater1.DataSource = que; Repeater1.DataBind(); }
綁定完成:
四、Repeater的編輯和刪除按鈕
在表格中相應的位置添加LinkButton控制項,然後給控制項設定 CommandName(為控制項設定名字) CommandArgument(綁定資料,索引)
<ItemTemplate> <tr> <td><%#Eval("Name") %></td> <td><%#Eval("PostCode") %></td> <td> <asp:LinkButton ID="LinkButton_guanli" CommandArgument=‘<%#Eval("ParentId") %>‘ CommandName="xiaji" runat="server">下級管理</asp:LinkButton> </td> <td> <asp:LinkButton ID="LinkButton_shanchu" CommandArgument=‘<%#Eval("ParentId") %>‘ CommandName="shanchu" runat="server">刪除</asp:LinkButton> </td> </tr> </ItemTemplate>
在Repeater控制項的屬性--事件—ItemCommand(單擊Repeater任意按鈕時觸發),雙擊自動建置事件
cs中的代碼:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { //刪除 if (e.CommandName == "shanchu")//如果點擊的是刪除按鈕 {//表中ParentId列的資料類型是int int code = int.Parse(e.CommandArgument.ToString()); Members cdata = diqu.Members.Single(r => r.ParentId == code); diqu.Members.DeleteOnSubmit(cdata); diqu.SubmitChanges(); binddata(); } //展示下級 if (e.CommandName == "xiaji") { int cou = e.CommandArgument.ToString().Length;//擷取字串長度,以判斷省市地區的層級 if (cou == 2)//省或者直轄市為兩位元 { Repeater1.DataSource = null;//清空資料 string code = e.CommandArgument.ToString(); //按照字元長度並截取字串進行匹配(搜尋的位置是 市) var xia = from m in diqu.Members where m.ParentId.ToString().Length == 4 where m.ParentId.ToString().Substring(0, 2) == code select m; Repeater1.DataSource = xia; Repeater1.DataBind(); } if (cou == 4)//市 為4位元 { Repeater1.DataSource = null;//清空資料 string code = e.CommandArgument.ToString(); //按照字元長度並截取字串進行匹配(搜尋的位置是 地區) var xia = from m in diqu.Members where m.ParentId.ToString().Length == 6 where m.ParentId.ToString().Substring(0, 4) == code select m; Repeater1.DataSource = xia; Repeater1.DataBind(); } if (cou == 6)//地區 為6位元,最小層級 { //彈出提示資訊 Response.Write("<script>alert(‘已到最小層級!‘);</script>"); } } }
20150301—ASP.NET的Repeater