- <%@ 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#代码
- 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();
- }
- }
运行结果
当点击view 连接时,可在目标页面获取 id 值
- 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"]) ;
- }
- }