Repeater控件结合UpdatePanel实现Ajax分页和删除功能
生活随笔
收集整理的這篇文章主要介紹了
Repeater控件结合UpdatePanel实现Ajax分页和删除功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本人一直抱怨使用GridView來實現分頁,刪除等功能,GridView效率低下(雖然我們什么基本什么都不用做),而且不是很靈活,最近才發現Repeater控件實在是一個不錯的控件。
??????首先給GridView,DataList和Repeater這3大數據綁定控件作下比較。三者都能夠綁定數據源,而不用去手動構造循環結構,GridView會自動生成許多布局控制,而DataList控件靈活性很好,它使用<table>進行數據展示(自動生成),但是現在的頁面布局都傾向于用DIV來布局,Repeater則不會自動生成任何標簽,它只用于綁定數據,我們可以用我們想要的方式去為它布局。顯而易見,GridView因為生成很多標簽,效率最差;DataList僅生成少量標簽,效率遠遠高于GridView;Repeater不會生成任何標簽,效率就屬它最高了。
??????說了這么多,還是來個簡單點的例子吧。
??????首先前臺代碼:
Code
?1<%@?Page?Language="C#"?AutoEventWireup="true"?CodeFile="index.aspx.cs"?Inherits="index"?%>
?2
?3<%@?Register?Assembly="System.Web.Extensions,?Version=1.0.61025.0,?Culture=neutral,?PublicKeyToken=31bf3856ad364e35"
?4????Namespace="System.Web.UI"?TagPrefix="asp"?%>
?5<%@?Import?Namespace="System.Data"?%>
?6<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
?7
?8<html?xmlns="http://www.w3.org/1999/xhtml"?>
?9<head?runat="server">
10????<title>無標題頁</title>
11</head>
12<body>
13????<form?id="form1"?runat="server">
14????<asp:ScriptManager?ID="ScriptManager1"?runat="server">
15????</asp:ScriptManager>
16???????<asp:UpdatePanel?ID="UpdatePanel1"?runat="server">
17????????<ContentTemplate>
18????????<asp:Repeater?ID="Repeater1"?runat="server">
19????????<HeaderTemplate>
20????????????<table>
21????????????????<tr>
22????????????????????<td>編號</td>
23????????????????????<td>姓名</td>
24????????????????????<td>時間</td>
25????????????????????<td>刪除</td>
26????????????????</tr>
27????????</HeaderTemplate>
28????????<ItemTemplate>
29????????????<tr>
30????????????????<td><%#((DataRowView)Container.DataItem)["id"]?%></td>
31????????????????<td><%#((DataRowView)Container.DataItem)["name"]?%></td>
32????????????????<td><%#DataBinder.Eval(Container.DataItem,?"hiredate","{0:yyyy年MM月dd日}")%></td>
33????????????????<td>
34????????????????????<asp:Button?ID="BtnDel"?runat="server"?Text="刪除"?OnCommand="BtnDel_Click"?CommandName=<%#((DataRowView)Container.DataItem)["id"]?%>></asp:Button></td>
35????????????</tr>
36????????</ItemTemplate>
37????????<FooterTemplate>
38????????????</table>
39????????</FooterTemplate>
40????????</asp:Repeater>
41????????當前頁:<asp:Label?ID="currentpage"?runat="server"></asp:Label>
42????????<asp:Button?ID="up"?runat="server"?Text="上一頁"?OnClick="up_Click"?/>
43????????<asp:Button?ID="down"?runat="server"?Text="下一頁"?OnClick="down_Click"?/>
44????</ContentTemplate>
45????</asp:UpdatePanel>
46????</form>
47</body>
48</html>
49??????上面的代碼很簡單,作下簡單說明:
??????Container.DataItem相當于某個表或視圖中的一行(Object類型),在得到該行的某個字段值時首先應該轉換成DataRowView(DataRowView 對象將值公開為對象數組,這些數組按基礎表中列的名稱或序號引用來編制索引),引用DataRowView要用到System.Data命名空間,DataBinder.Eval()是另外一種綁定數據的方式,在這里是為了格式化日期(去除數據庫中的時分秒,只顯示年月日)。
關于數據綁定可以看李濤的博客http://www.cnblogs.com/terryli/archive/2008/03/25/1120482.html,這里面講的很詳細。
??????然后來看后臺代碼:
?????? Code
using?System;
using?System.Data;
using?System.Configuration;
using?System.Collections;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?System.Web.UI.HtmlControls;
using?System.Data.SqlClient;
public?partial?class?index?:?System.Web.UI.Page
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????if?(!Page.IsPostBack)
????????{
????????????//頁面加載時當前頁是第一頁
????????????currentpage.Text?=?"1";
????????????//綁定數據?
????????????RepeatData();??????????
????????}
????}
????public?void?RepeatData()
????{
????????//連接數據庫
????????string?connString?=?"server=.\\sql2005;uid=sa;pwd=sa2005;database=test";
????????SqlConnection?conneciotn?=?new?SqlConnection(connString);
????????//填充數據
????????SqlDataAdapter?sda?=?new?SqlDataAdapter("select?*?from?infos",conneciotn);
????????DataSet?ds?=?new?DataSet();
????????sda.Fill(ds);
????????//運用分頁控件
????????PagedDataSource?pds?=?new?PagedDataSource();
????????//給分頁控件數據源
????????pds.DataSource?=?ds.Tables[0].DefaultView;
????????//允許分頁
????????pds.AllowPaging?=?true;
????????//每頁顯示5條記錄
????????pds.PageSize?=?5;
????????//分頁控件的分頁索引
????????pds.CurrentPageIndex?=?Convert.ToInt32(currentpage.Text)-1;
????????this.up.Enabled?=?true;
????????this.down.Enabled?=?true;
????????//首頁,上一頁按鈕變灰
????????if(pds.IsFirstPage)
????????{
????????????this.up.Enabled?=?false;
????????}
????????//尾頁,下一頁按鈕變灰
????????if(pds.IsLastPage)
????????{
????????????this.down.Enabled?=?false;
????????}
????????//最后未Repeater控件綁定數據源
????????this.Repeater1.DataSource?=?pds;
????????this.Repeater1.DataBind();
????}
????//點擊上一頁按鈕的動作
????protected?void?up_Click(object?sender,?EventArgs?e)
????{
????????this.currentpage.Text?=?Convert.ToString(Convert.ToInt32(this.currentpage.Text)?-?1);
????????RepeatData();
????}
????//點擊下一頁按鈕的動作
????protected?void?down_Click(object?sender,?EventArgs?e)
????{
????????this.currentpage.Text?=?Convert.ToString(Convert.ToInt32(this.currentpage.Text)?+?1);
????????RepeatData();
????????
????}
????//刪除按鈕,刪除該行,該事件和前臺的OnCommand對應
????protected?void?BtnDel_Click(object?sender,?CommandEventArgs?e)
????{
????????//e.Command獲取當前這一行數據的id,id在表中為主鍵
????????int?id?=?Convert.ToInt32(e.CommandName);
????????string?connString?=?"server=.\\sql2005;uid=sa;pwd=sa2005;database=test";
????????SqlConnection?conneciotn?=?new?SqlConnection(connString);
????????string?sql?=?"delete?from?infos?where?id?=?"?+?id;
????????SqlCommand?sqlcmd?=?new?SqlCommand(sql,?conneciotn);
????????sqlcmd.Connection.Open();
????????int?result?=?sqlcmd.ExecuteNonQuery();
????????RepeatData();
????}
}
??????同樣要實現行的修改,類似于行的刪除。Repeater控件需要我們記的東西不多,用起來也極為方便。
??????首先給GridView,DataList和Repeater這3大數據綁定控件作下比較。三者都能夠綁定數據源,而不用去手動構造循環結構,GridView會自動生成許多布局控制,而DataList控件靈活性很好,它使用<table>進行數據展示(自動生成),但是現在的頁面布局都傾向于用DIV來布局,Repeater則不會自動生成任何標簽,它只用于綁定數據,我們可以用我們想要的方式去為它布局。顯而易見,GridView因為生成很多標簽,效率最差;DataList僅生成少量標簽,效率遠遠高于GridView;Repeater不會生成任何標簽,效率就屬它最高了。
??????說了這么多,還是來個簡單點的例子吧。
??????首先前臺代碼:
Code
?1<%@?Page?Language="C#"?AutoEventWireup="true"?CodeFile="index.aspx.cs"?Inherits="index"?%>
?2
?3<%@?Register?Assembly="System.Web.Extensions,?Version=1.0.61025.0,?Culture=neutral,?PublicKeyToken=31bf3856ad364e35"
?4????Namespace="System.Web.UI"?TagPrefix="asp"?%>
?5<%@?Import?Namespace="System.Data"?%>
?6<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
?7
?8<html?xmlns="http://www.w3.org/1999/xhtml"?>
?9<head?runat="server">
10????<title>無標題頁</title>
11</head>
12<body>
13????<form?id="form1"?runat="server">
14????<asp:ScriptManager?ID="ScriptManager1"?runat="server">
15????</asp:ScriptManager>
16???????<asp:UpdatePanel?ID="UpdatePanel1"?runat="server">
17????????<ContentTemplate>
18????????<asp:Repeater?ID="Repeater1"?runat="server">
19????????<HeaderTemplate>
20????????????<table>
21????????????????<tr>
22????????????????????<td>編號</td>
23????????????????????<td>姓名</td>
24????????????????????<td>時間</td>
25????????????????????<td>刪除</td>
26????????????????</tr>
27????????</HeaderTemplate>
28????????<ItemTemplate>
29????????????<tr>
30????????????????<td><%#((DataRowView)Container.DataItem)["id"]?%></td>
31????????????????<td><%#((DataRowView)Container.DataItem)["name"]?%></td>
32????????????????<td><%#DataBinder.Eval(Container.DataItem,?"hiredate","{0:yyyy年MM月dd日}")%></td>
33????????????????<td>
34????????????????????<asp:Button?ID="BtnDel"?runat="server"?Text="刪除"?OnCommand="BtnDel_Click"?CommandName=<%#((DataRowView)Container.DataItem)["id"]?%>></asp:Button></td>
35????????????</tr>
36????????</ItemTemplate>
37????????<FooterTemplate>
38????????????</table>
39????????</FooterTemplate>
40????????</asp:Repeater>
41????????當前頁:<asp:Label?ID="currentpage"?runat="server"></asp:Label>
42????????<asp:Button?ID="up"?runat="server"?Text="上一頁"?OnClick="up_Click"?/>
43????????<asp:Button?ID="down"?runat="server"?Text="下一頁"?OnClick="down_Click"?/>
44????</ContentTemplate>
45????</asp:UpdatePanel>
46????</form>
47</body>
48</html>
49??????上面的代碼很簡單,作下簡單說明:
??????Container.DataItem相當于某個表或視圖中的一行(Object類型),在得到該行的某個字段值時首先應該轉換成DataRowView(DataRowView 對象將值公開為對象數組,這些數組按基礎表中列的名稱或序號引用來編制索引),引用DataRowView要用到System.Data命名空間,DataBinder.Eval()是另外一種綁定數據的方式,在這里是為了格式化日期(去除數據庫中的時分秒,只顯示年月日)。
關于數據綁定可以看李濤的博客http://www.cnblogs.com/terryli/archive/2008/03/25/1120482.html,這里面講的很詳細。
??????然后來看后臺代碼:
?????? Code
using?System;
using?System.Data;
using?System.Configuration;
using?System.Collections;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?System.Web.UI.HtmlControls;
using?System.Data.SqlClient;
public?partial?class?index?:?System.Web.UI.Page
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????????if?(!Page.IsPostBack)
????????{
????????????//頁面加載時當前頁是第一頁
????????????currentpage.Text?=?"1";
????????????//綁定數據?
????????????RepeatData();??????????
????????}
????}
????public?void?RepeatData()
????{
????????//連接數據庫
????????string?connString?=?"server=.\\sql2005;uid=sa;pwd=sa2005;database=test";
????????SqlConnection?conneciotn?=?new?SqlConnection(connString);
????????//填充數據
????????SqlDataAdapter?sda?=?new?SqlDataAdapter("select?*?from?infos",conneciotn);
????????DataSet?ds?=?new?DataSet();
????????sda.Fill(ds);
????????//運用分頁控件
????????PagedDataSource?pds?=?new?PagedDataSource();
????????//給分頁控件數據源
????????pds.DataSource?=?ds.Tables[0].DefaultView;
????????//允許分頁
????????pds.AllowPaging?=?true;
????????//每頁顯示5條記錄
????????pds.PageSize?=?5;
????????//分頁控件的分頁索引
????????pds.CurrentPageIndex?=?Convert.ToInt32(currentpage.Text)-1;
????????this.up.Enabled?=?true;
????????this.down.Enabled?=?true;
????????//首頁,上一頁按鈕變灰
????????if(pds.IsFirstPage)
????????{
????????????this.up.Enabled?=?false;
????????}
????????//尾頁,下一頁按鈕變灰
????????if(pds.IsLastPage)
????????{
????????????this.down.Enabled?=?false;
????????}
????????//最后未Repeater控件綁定數據源
????????this.Repeater1.DataSource?=?pds;
????????this.Repeater1.DataBind();
????}
????//點擊上一頁按鈕的動作
????protected?void?up_Click(object?sender,?EventArgs?e)
????{
????????this.currentpage.Text?=?Convert.ToString(Convert.ToInt32(this.currentpage.Text)?-?1);
????????RepeatData();
????}
????//點擊下一頁按鈕的動作
????protected?void?down_Click(object?sender,?EventArgs?e)
????{
????????this.currentpage.Text?=?Convert.ToString(Convert.ToInt32(this.currentpage.Text)?+?1);
????????RepeatData();
????????
????}
????//刪除按鈕,刪除該行,該事件和前臺的OnCommand對應
????protected?void?BtnDel_Click(object?sender,?CommandEventArgs?e)
????{
????????//e.Command獲取當前這一行數據的id,id在表中為主鍵
????????int?id?=?Convert.ToInt32(e.CommandName);
????????string?connString?=?"server=.\\sql2005;uid=sa;pwd=sa2005;database=test";
????????SqlConnection?conneciotn?=?new?SqlConnection(connString);
????????string?sql?=?"delete?from?infos?where?id?=?"?+?id;
????????SqlCommand?sqlcmd?=?new?SqlCommand(sql,?conneciotn);
????????sqlcmd.Connection.Open();
????????int?result?=?sqlcmd.ExecuteNonQuery();
????????RepeatData();
????}
}
??????同樣要實現行的修改,類似于行的刪除。Repeater控件需要我們記的東西不多,用起來也極為方便。
轉載于:https://www.cnblogs.com/psunny/archive/2009/07/28/1533496.html
總結
以上是生活随笔為你收集整理的Repeater控件结合UpdatePanel实现Ajax分页和删除功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大神们,中国健力宝有哪些产品是电解质饮料
- 下一篇: 广宁省安兴社豆河工业区晶科工程邮政编码是