sharepoint绑定
绑定
1 private static PagedDataSource pds = new PagedDataSource(); 2 3 protected void Page_Load(object sender, EventArgs e) 4 { 5 if (!IsPostBack) 6 { 7 BindData(); 8 } 9 } 10 private void BindData() 11 { 12 var leaderMailBox = SharePointUtility.GetListItemDictionary<领导信箱>("LeaderMailBox"); 13 IEnumerable<领导信箱> LeaderMail = leaderMailBox.Values 14 .Where(item => item.回复是否显示 == "是") 15 .OrderByDescending(item => item.提交日期); 16 17 int newsCount = LeaderMail.Count(); 18 if (newsCount <=0) 19 { 20 Label1.Text = "暂无数据"; 21 } 22 else 23 { 24 Label1.Text = ""; 25 } 26 AspNetPager.RecordCount = newsCount; 27 AspNetPager.PageSize = 20; 28 pds.DataSource = LeaderMail.ToList(); 29 pds.AllowPaging = true; //数据源 允许分页 30 pds.CurrentPageIndex = AspNetPager.CurrentPageIndex - 1; //显示 当前页 31 pds.PageSize = AspNetPager.PageSize; //取控件 分页大小 32 BindToList(); 33 } 34 private void BindToList() 35 { 36 37 rpt_mailList.DataSource = pds; 38 rpt_mailList.DataBind(); 39 40 } 41 42 protected void AspNetPager_PageChanged(object src, EventArgs e) 43 { 44 pds.CurrentPageIndex = AspNetPager.CurrentPageIndex - 1; //显示 当前页 45 pds.PageSize = AspNetPager.PageSize; //取控件 分页大小 46 BindToList(); 47 }
上一页下一页
1 protected void PrePage_Click(object sender, EventArgs e) 2 { 3 //string SiteUrl = SPContext.Current.Web.Url; 4 //using (SPLinqDataContext SiteData = new SPLinqDataContext(SiteUrl)) 5 //{ 6 7 var LeaderMailFilter = LeaderMail 8 .Where(item => item.回复是否显示 == "是") 9 .OrderByDescending(item => item.提交日期) 10 .ToList(); 11 int index = 12 LeaderMailFilter.FindIndex(item => item.ID == Convert.ToInt32(ViewState["ID"])); 13 if (index > 0) 14 { 15 index--; 16 int ID = LeaderMailFilter.ElementAt(index).ID; 17 BindData(ID); 18 } 19 else 20 { 21 mailTitle = Convert.ToString(ViewState["Title"]); 22 } 23 //} 24 } 25 26 protected void NextPage_Click(object sender, EventArgs e) 27 { 28 var LeaderMailFilter = LeaderMail 29 .Where(item => item.回复是否显示 == "是") 30 .OrderByDescending(item => item.提交日期) 31 .ToList(); 32 int index = 33 LeaderMailFilter.FindIndex(item => item.ID == Convert.ToInt32(ViewState["ID"])); 34 //int index = LeaderMail.ToList().FindIndex(item => item.ID == Convert.ToInt32(ViewState["ID"]) && item.处理结果=="显示"); 35 36 if (index < LeaderMailFilter.Count() - 1) 37 { 38 index++; 39 int ID = LeaderMailFilter.ElementAt(index).ID; 40 BindData(ID); 41 } 42 else 43 { 44 mailTitle = Convert.ToString(ViewState["Title"]); 45 } 46 47 }
SharePointUtility
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using Microsoft.SharePoint; 6 using System.Diagnostics; 7 using System.Web; 8 using Microsoft.SharePoint.WebControls; 9 using System.IO; 10 11 namespace Cbw_SinopecPortal_WebPart 12 { 13 class SharePointUtility 14 { 15 public static int GetLookupID(object field) 16 { 17 string fieldValue = field.ToString(); 18 return Convert.ToInt32(fieldValue.Substring(0, fieldValue.IndexOf(";#"))); 19 } 20 21 public static string GetLookupLink(object field) 22 { 23 string fieldValue = Convert.ToString(field); 24 if (string.IsNullOrEmpty(fieldValue)) 25 { 26 return null; 27 } 28 else 29 { 30 return fieldValue.Substring(0, fieldValue.IndexOf(", ")); 31 } 32 } 33 34 public static SPList GetList(string listName) 35 { 36 listName = "Lists/" + listName; 37 foreach (SPList list in SPContext.Current.Web.Lists) 38 { 39 if (list.RootFolder.Url == listName) 40 { 41 return list; 42 } 43 } 44 throw new Exception("不存在指定的列表:" + listName); 45 } 46 47 public static SPList GetDocumentLibaray(string libarary) 48 { 49 foreach (SPList list in SPContext.Current.Web.Lists) 50 { 51 if (list.RootFolder.Url == libarary) 52 { 53 return list; 54 } 55 } 56 throw new Exception("不存在指定的文檔庫:" + libarary); 57 } 58 59 public static SPList GetListOrDocumentLibaray(string name) 60 { 61 string listName = "Lists/" + name; 62 foreach (SPList list in SPContext.Current.Web.Lists) 63 { 64 if (list.RootFolder.Url == listName || list.RootFolder.Url == name) 65 { 66 return list; 67 } 68 } 69 throw new Exception("不存在指定的列表OR文檔庫:" + name); 70 } 71 72 public static Dictionary<int, T> GetListItemDictionary<T>(string listName) 73 where T : Entity.Item, new() 74 { 75 var list = SharePointUtility.GetList(listName); 76 var items = from item in list.GetItems(new SPQuery()).Cast<SPListItem>() 77 select new { item.ID, Value = Entity.Item.Create<T>(item) }; 78 return items.ToDictionary(item => item.ID, item => item.Value); 79 } 80 81 public static Dictionary<int, T> GetListItemDictionary<T>(string listName, SPQuery spquery) 82 where T : Entity.Item, new() 83 { 84 var list = SharePointUtility.GetList(listName); 85 var items = from item in list.GetItems(spquery).Cast<SPListItem>() 86 select new { item.ID, Value = Entity.Item.Create<T>(item) }; 87 return items.ToDictionary(item => item.ID, item => item.Value); 88 } 89 90 public static Dictionary<int, T> GetDocumentItemDictionary<T>(string listName) 91 where T : Entity.Item, new() 92 { 93 var list = SharePointUtility.GetDocumentLibaray(listName); 94 var items = from item in list.GetItems(new SPQuery()).Cast<SPListItem>() 95 select new { item.ID, Value = Entity.Item.Create<T>(item) }; 96 return items.ToDictionary(item => item.ID, item => item.Value); 97 } 98 99 public static Dictionary<int, T> GetFolderDictionary<T>(string listName) 100 where T : Entity.Item, new() 101 { 102 var list = SharePointUtility.GetDocumentLibaray(listName); 103 var items = from item in list.Folders.Cast<SPListItem>() 104 select new { item.ID, Value = Entity.Item.Create<T>(item) }; 105 return items.ToDictionary(item => item.ID, item => item.Value); 106 } 107 108 public static T FindDocLibObj<T>(int ID, IEnumerable<T> Content) 109 where T : Entity.DocumentLibrary<T>, new() 110 { 111 var item = Content.Where(p => p.ID == ID).FirstOrDefault(); 112 if (item != null) 113 { 114 return item; 115 } 116 foreach (var contentItem in Content.Where(p => p.IsFolder)) 117 { 118 T result = FindDocLibObj(ID, contentItem.FolderCotent.Values); 119 if (result != null) 120 { 121 return result; 122 } 123 } 124 return null; 125 } 126 127 public static int GetTotalSubFileCountForCollection<T>(IEnumerable<T> Collection) 128 where T : Entity.DocumentLibrary<T>, new() 129 { 130 int TempSum = 0; 131 foreach (var item in Collection.Where(item => item.IsFolder)) 132 { 133 TempSum += GetTotalSubFileCountForFolder(item); 134 } 135 return TempSum + Collection.Count(p => !p.IsFolder); 136 } 137 138 public static int GetTotalSubFileCountForFolder<T>(T DocLibObj) 139 where T : Entity.DocumentLibrary<T>, new() 140 { 141 int TempSum = 0; 142 foreach (var DocLibObjItem in DocLibObj.FolderCotent.Values.Where(p => p.IsFolder)) 143 { 144 TempSum += GetTotalSubFileCountForFolder(DocLibObjItem); 145 } 146 return TempSum + DocLibObj.FolderCotent.Values.Count(p => !p.IsFolder); 147 } 148 149 public static string GetAttachmentUrl(SPAttachmentCollection Attachments) 150 { 151 if (Attachments != null && Attachments.Count > 0) 152 { 153 return Attachments.UrlPrefix + Attachments[0]; 154 } 155 return null; 156 } 157 158 public static void WriteLog(Exception ex) 159 { 160 try 161 { 162 Debug.WriteLine(ex); 163 SPSecurity.RunWithElevatedPrivileges(delegate() 164 { 165 EventLog.WriteEntry("Cbw_SinopecPortal", ex.ToString()); 166 }); 167 } 168 catch 169 { 170 171 } 172 } 173 174 public static void PrivateLogin(string siteUrl) 175 { 176 SPSite site = new SPSite(siteUrl); 177 SPWeb web = site.OpenWeb(); 178 HttpRequest httpRequest = new HttpRequest("", web.Url, ""); 179 HttpContext.Current = new HttpContext(httpRequest, new HttpResponse(new StringWriter())); 180 SPControl.SetContextWeb(HttpContext.Current, web); 181 } 182 } 183 }