using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; public partial class SysAdmin_SortManager : System.Web.UI.Page { public String Cid; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { myRepeater.DataSource = pds(); //为Repeater1控件指定数据源 myRepeater.DataBind(); //这个当然是必须的,绑定上去xxxxxxx } } private PagedDataSource pds() { SqlConnection myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString.ToString()); myconn.Open(); SqlDataAdapter da = new SqlDataAdapter("select * from v3_m_category where ParentId =0", myconn); DataSet ds = new DataSet(); da.Fill(ds, "user"); SqlDataAdapter da2 = new SqlDataAdapter("select * from v3_m_category where ParentId !=0", myconn); da2.Fill(ds, "class"); ds.Relations.Add("myrela", ds.Tables["user"].Columns["id"], ds.Tables["class"].Columns["ParentId"]); //为上面建立的两个表创建一个关系,指明父列和子列的名称并为他们的关系命名 PagedDataSource pds = new PagedDataSource(); //初始化一个PagedDataSource,允许控件分页 pds.DataSource = ds.Relations["myrela"].ParentTable.DefaultView; //将上面的ds转换成标准数据视图; pds.AllowPaging = true; //允许分页 pds.PageSize = 1; //每页大小为5 pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]); //设置当前页 return pds; //将处理完毕的pds对象发出去 } protected void myRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item) { Repeater r = (Repeater)e.Item.FindControl("myRepeater2"); r.ItemCommand += new RepeaterCommandEventHandler(r_ItemCommand); } #region 这个是分页的 if (e.Item.ItemType == ListItemType.Footer) {//判断当前项是页脚模板 int n = pds().PageCount;//将分页总数赋给变量n int i = pds().CurrentPageIndex;//将当前分页码赋给i Label lblpc = (Label)e.Item.FindControl("lblpc"); lblpc.Text = n.ToString(); //找到lblpc这个Label,将总页码赋给他 Label lblp = (Label)e.Item.FindControl("lblp"); lblp.Text = Convert.ToString(pds().CurrentPageIndex + 1); //找到lblp这个Label,将当前页码赋给他,但是注意,因为页码从0开始,这里要直观的话就得加1 HyperLink hlfir = (HyperLink)e.Item.FindControl("hlfir"); hlfir.NavigateUrl = "?page=0"; HyperLink hlla = (HyperLink)e.Item.FindControl("hlla"); hlla.NavigateUrl = "?page=" + Convert.ToInt32(n - 1); //找到表示最前页和末页的Label,为他们的NavigateUrl属性赋为第0页和最大页码减1 HyperLink hlp = (HyperLink)e.Item.FindControl("hlp"); HyperLink hln = (HyperLink)e.Item.FindControl("hln"); //找到表示上页和下页这两个控件 if (i <= 0) {//如果当前页已经是第0页 hlfir.Enabled = false; hlp.Enabled = false; hln.Enabled = true; } else { hlp.NavigateUrl = "?page=" + Convert.ToInt32(i - 1); } if (i > n - 2) {//如果当前项已经是最末页 hln.Enabled = false; hlla.Enabled = false; hlp.Enabled = true; } else { hln.NavigateUrl = "?page=" + Convert.ToInt32(i + 1); } DropDownList ddlpage = (DropDownList)e.Item.FindControl("ddlpage"); for (int p = 0; p < n; p++) { ddlpage.Items.Add(Convert.ToString(p + 1)); if (p == i) { ddlpage.SelectedIndex = p; } } } #endregion } protected void ddl_SelectedChanged(object sender, EventArgs e) { DropDownList ddl = (DropDownList)sender; Page.Response.Redirect("SortManager.aspx?page=" + (Convert.ToInt32(ddl.SelectedItem.Value) - 1)); } protected void myRepeater_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName == "update") { } else if (e.CommandName == "delete") { string delstr = "delete from v3_m_category where id = " + e.CommandArgument + " or parentid = " + e.CommandArgument; bool result = new common().ExcuteNoneQuery(delstr, null); if (result) { Methods1.Methods1.MsgBox("删除成功!",Request.Url.AbsolutePath ); } } } private void r_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName == "update") { } else if (e.CommandName == "delete") { string delstr = "delete from v3_m_category where id = " + e.CommandArgument; bool result = new common().ExcuteNoneQuery(delstr, null); if (result) { Methods1.Methods1.MsgBox("删除成功!",Request.Url.AbsolutePath); } } } }