Site9527EntitiesDataContext (重要的类):连接实体与网站List操作
SPContext.Current.Web.Url:获取当前操作的页面
FirstOrDefault:使用时一定要先引用(Using System.Linq;)
dataContext.SubmitChanges():确保数据更新到List中。
增
protected void bt_SaveClic(object sender, EventArgs e) { using (Site9527EntitiesDataContext dataContext = new Site9527EntitiesDataContext(SPContext.Current.Web.Url)) { UserInfoItem user = new UserInfoItem(); user.Title = this.tx_Title.Text; user .UserName = this .tx_UserName.Text; user.Gender = this.tx_Gender.Text; user.Adress =this. tx_adress.Text; dataContext.UserInfo.InsertOnSumbit(user); dataContext.SubmitChangers(); } base.Response.Redirect("UserInfoList.aspx"); }
删
protected void bt_DeleteClick(object sender, EventArgs e) { using (Site9527EntitiesDataContext dataContext = new Site9527EntitiesDataContext(SPContext.Current.Web.Url)) { UserInfoItem user = null; user = dataContext.UserInfo.FirstOrDefault(i => i.Id ==Convert.ToInt32(tx_Del.Text)); dataContext.UserInfo.DeleteOnSubmit(user); dataContext.SubmitChanges(); } tx_Del.Text = ""; bt_SelectClick(sender, e); }
查
public void bt_SelectClick(object sender, EventArgs e) { StringBuilder html = new StringBuilder(); IQueryable<UserInfoItem> userList=null; if (!string.IsNullOrEmpty(tb_UserName.Text)) { using(Site9527EntitiesDataContext dataContext=new Site9527EntitiesDataContext(SPContext.Current.Web.Url)) { userList = dataContext.UserInfo.Where(i => i.UserName.Contains(tb_UserName.Text)); } } else { using(Site9527EntitiesDataContext dataContext=new Site9527EntitiesDataContext(SPContext.Current.Web.Url)) { userList = dataContext.UserInfo; } } foreach (var user in userList) { html.Append("<tr>"); html.Append("<td>"+user.Title); html.Append("</td>"); html.Append("<td>" + user.UserName); html.Append("</td>"); html.Append("<td>" + user.Gender); html.Append("</td>"); html.Append("<td>" + user.Tell); html.Append("</td>"); html.Append("<td>" + user.Adress); html.Append("</td>"); html.Append("<td>" + user.ApprovalStatue); html.Append("</td>"); html.Append("<td>" + user.ApprovalBy); html.Append("</td>"); html.Append("<td><a id=‘a_update‘ onclick=‘Update(" + user.Id.ToString() + ")‘>更新</a> <a id=‘a_delete‘ onclick=‘Delete(" + user.Id.ToString() + ")‘>删除</a>"); html.Append("</td>"); html.Append("</tr>"); } td_Html.Text = html.ToString(); }
改
protected void bt_SaveClic(object sender, EventArgs e) { using (Site9527EntitiesDataContext dataContext = new Site9527EntitiesDataContext(SpContext.Current.Web.Url)) { UserInfoItem user = null; user = dataContext.UserInfo.FirstOrDefault(i => i.Id == Convert.ToInt32(Request.QueryString["ID"])); user.Title = this.tx_Title.Text; user.UserName = this.tx_UserName.Text; user.Adress = this.tx_adress.Text; user.Gender = this.tx_Gender.Text; user.Tell = this.tx_tell.Text; dataContext.SubmitChanges(user); } base.Response.Redirent("UserInfoList.aspx"); }
sharepoint Linq方式的增,删,查,改
时间: 2024-11-09 16:45:48