- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <script language="javascript" type="text/ecmascript">
- function Check(ParentChk, ChildId) {
- var oElements = document.getElementsByTagName("INPUT");//獲取全部 input元素
- var bIsChecked = ParentChk.checked;
- for(var i=0;i<oElements.length;i++)
- {
- if (IsCheckBox(oElements[i]) && IsMatch(oElements[i].id, ChildId)) //是checkbox 而且是 repeater 內的符合條件的input 元素
- {
- oElements[i].checked=bIsChecked; //將全部的 checkbox 狀態置爲選中
- }
- }
- }
- function IsMatch(id,ChidId) //是不是repeater 下的某個子元素
- {
- var sPattern = '^Rept1.*' + ChidId + '$';
- var oRegExp=new RegExp(sPattern);
- if(oRegExp.exec(id))
- return true;
- else
- return false;
- }
- function IsCheckBox(chk) // 判斷是不是 checkbox
- {
- if(chk.type=='checkbox')return true;
- else return false;
- }
- </script>
- </head>
- <body>
- <form id="form1" runat="server" title="自定義表結構">
- <div>
- <asp:Repeater ID="Rept1" runat="server">
- <HeaderTemplate> <%-- 定義標題--%>
- <table border="0" cellpadding="0" cellspacing="0" style="width:1006px;border-collapse:collapse; text-align:center;">
- <tr>
- <td style="background-color:#cccccc; font-weight:bold; height:25px;">
- <input id="chkAll" name="chkAll" runat="server" type="checkbox" onclick="Check(this,'chkItem')" title="全選" />全</td>
- <td style="background-color:#cccccc; font-weight:bold; height:25px;">View</td>
- <td style="background-color:#cccccc; font-weight:bold; height:25px;" visible="false"></td>
- <td style="background-color:#cccccc; font-weight:bold;">名字</td>
- <td style="background-color:#cccccc; font-weight:bold;">班級</td>
- </tr>
- </HeaderTemplate>
- <ItemTemplate> <%-- <ItemTemplate> 元素的內容會對應 DataSet 中的 "record" 重複出現--%>
- <tr>
- <%-- <td >張三</td> 能夠直接定義表數據
- <td>07一班</td>--%>
- <td><asp:CheckBox ID="chkItem" runat="server"/></td>
- <td><a href='<%#"ViewDetail.aspx?id="+DataBinder.Eval(Container.DataItem,"id") %>' target="_blank">View</a></td> <%--鏈接 :將本記錄的行 Id 傳遞到目標頁面--%>
- <td><asp:Label ID ="lblID" Text='<%#DataBinder.Eval(Container.DataItem,"id") %>' runat="server" Visible="false"></asp:Label></td> <%-- 存儲本記錄的 id ,做爲刪除等操做的依據 --%>
- <td><%#DataBinder.Eval(Container.DataItem,"name") %></td> <%--從數據庫中取出表數據--%>
- <td><%#DataBinder.Eval(Container.DataItem,"class") %></td>
- </tr>
- </ItemTemplate>
- <AlternatingItemTemplate> <%--記錄顯示方式的交替樣式 , 基本與 ItemTemplate 樣式一致--%>
- <tr bgcolor="#e8e8e8">
- <td><asp:CheckBox ID="chkItem" runat="server"/></td>
- <td><a href='<%#"Default.aspx?id="+DataBinder.Eval(Container.DataItem,"id") %>' target="_blank">View</a></td>
- <td><asp:Label ID ="lblID" Text='<%#DataBinder.Eval(Container.DataItem,"id") %>' runat="server" Visible="false"></asp:Label></td>
- <td><%#DataBinder.Eval(Container.DataItem,"name") %></td>
- <td><%#DataBinder.Eval(Container.DataItem,"class")%></td>
- </tr>
- </AlternatingItemTemplate>
- <%-- <SeparatorTemplate>--%> <%--可以用於描述每一個記錄之間的分隔符 --%>
- <%-- <tr>
- <td colspan="0"><hr /></td>--%> <%-- 在每一個表格行之間插入了一條水平線--%>
- <%-- </tr>
- </SeparatorTemplate>--%>
- <FooterTemplate>
- </table>
- </FooterTemplate>
- <FooterTemplate> <%--<FooterTemplate> 的內容在輸出中僅出現一次--%>
- </table>
- </FooterTemplate>
- </asp:Repeater>
- <div style="background-color:#dedede; width:1006px;">
- <asp:Button ID="btnDel" runat="server" Text="刪除" onclick="btnDel_Click" />
- <asp:label ID="lblCurrentPage" runat="server"></asp:label>
- <asp:HyperLink id="lnkFrist" runat="server">首頁</asp:HyperLink>
- <asp:HyperLink id="lnkPrev" runat="server">上一頁</asp:HyperLink>
- <asp:HyperLink id="lnkNext" runat="server">下一頁</asp:HyperLink>
- <asp:HyperLink id="lnkEnd" runat="server">尾頁</asp:HyperLink>
- </div>
- </div>
- </form>
- </body>
- </html>
C#代碼javascript
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Configuration;
- using System.Data.SqlClient;
- using System.Data;
- public partial class Default4 : System.Web.UI.Page
- {
- PagedDataSource PDS = new PagedDataSource(); //PagedDataSource 實現分頁
- private string StrConn = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString.ToString();
- private string strSql = "";
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- BindRepeater();
- }
- private void BindRepeater()
- {
- int totalCount = 0;//總記錄條數
- int totalPage = 1;//總頁數
- string sql = "select id ,name,class from Category";
- DataSet ds=new DataSet ();
- SqlConnection conn = new SqlConnection(StrConn);
- conn.Open();
- SqlDataAdapter da = new SqlDataAdapter(sql, conn);
- da.Fill(ds);
- conn.Close();
- totalCount = ds.Tables[0].DefaultView.Count;
- PDS.DataSource = ds.Tables[0].DefaultView; //設置pagedatasource 的屬性
- PDS.AllowPaging = true; //是否容許分頁
- PDS.PageSize = 5;//每頁顯示記錄數量
- int curPage;
- if(Request.QueryString["page"]!=null)
- curPage=Convert.ToInt32(Request.QueryString["page"]);// 爲四個連接準備
- else
- curPage=1;
- if (totalCount ==0)//若是沒有記錄,就一頁
- totalPage = 1;
- else
- {
- if (totalCount % PDS.PageSize == 0) //若是是整數頁
- totalPage = totalCount / PDS.PageSize;
- else
- totalPage = totalCount / PDS.PageSize + 1; //若是非整數頁
- }
- PDS.CurrentPageIndex = curPage - 1;
- lblCurrentPage.Text = "共" + totalCount.ToString() + "條記錄,當前頁" + curPage.ToString() + "/" + totalPage;//共30條記錄,當前頁1/2
- lnkFrist.NavigateUrl = Request.CurrentExecutionFilePath+"?page=1";//獲取當前請求的虛擬路徑
- if (!PDS.IsFirstPage) //若是不是第一頁
- lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToInt32(curPage - 1);
- if (!PDS.IsLastPage) //若是不是最後一頁
- lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToInt32(curPage + 1);
- lnkEnd.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + totalPage;
- Rept1.DataSource = PDS;
- Rept1.DataBind();
- }
- protected void btnDel_Click(object sender, EventArgs e)
- {
- string ID = "";
- for (int i = 0; i < this.Rept1.Items.Count; i++)
- {
- CheckBox cbox = (CheckBox)this.Rept1.Items[i].FindControl("chkItem");
- if (cbox.Checked == true) //選中複選框
- {
- if (ID == "")
- {
- ID = "'" + ((Label)this.Rept1.Items[i].FindControl("lblID")).Text + "'";//label中存儲字段 id
- }
- else
- {
- ID += ","+"'" + ((Label)this.Rept1.Items[i].FindControl("lblID")).Text + "'";
- }
- }
- }
- strSql = "delete from Category where id in(" + ID + ")"; //刪除選中的記錄行
- SqlConnection conn = new SqlConnection(StrConn);
- try
- {
- SqlCommand cmd = new SqlCommand(strSql, conn);
- cmd.Connection.Open();
- cmd.ExecuteNonQuery();
- cmd.Connection.Close();
- System.Web.HttpContext.Current.Response.Write("<script language='javascript'>alert('刪除成功!');</script>"); //刪除成功後,提示對話框
- }
- catch (System.Data.SqlClient.SqlException E)
- {
- throw new Exception(E.Message);
- }
- finally
- {
- if (conn.State == ConnectionState.Open)
- conn.Close();
- }
- this.BindRepeater();
- }
- }
運行結果html
當點擊view 鏈接時,可在目標頁面獲取 id 值java
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- public partial class ViewDetail : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- Response.Write("由頁面Default傳過來的記錄 ID 是 "+Request.QueryString["id"]) ;
- }
- }