SPContext.Current.Web.Lists["UserInfo"]:获取网站的List,名称是:UserInfo
userlist.AddItem():添加数据到List里
user.Update():将数据保存到List里,更新的List里的数据
GetItemById:获取当前行的ID
query.Query (XML的方式条件):过滤查询条件
增
protected void bt_SaveClick(object sender, EventArgs e) { SPList userlist = SPContext.Current.Web.Lists["UserInfo"]; SPListItem user = userlist.AddItem(); user["Title"] = this.tx_Title.Text; user["UserName"] = this.tx_UserName.Text; user["Gender"] = this.tx_Gender.Text; user["Tell"] = this.tx_tell.Text; user["Adress"] = this.tx_adress.Text; user.Update(); base.Response.Redirect("UserInfoList.aspx"); }
删
protected void bt_DeleteClick(object sender, EventArgs e) { using (Site9527EntitiesDataContext dataContext = new Site9527EntitiesDataContext(SPContext.Current.Web.Url)) { SPList userList = SPContext.Current.Web.Lists["UserInfo"]; SPListItem user = userList.GetItemById(Convert.ToInt32(tx_Del.Text)); user.Delete(); } tx_Del.Text = ""; bt_SelectClick(sender, e); }
查
public void bt_SelectClick(object sender, EventArgs e) { StringBuilder html = new StringBuilder(); SPList userList = SPContext.Current.Web.Lists["UserInfo"]; SPListItemCollection users = null; if (!string.IsNullOrEmpty(tb_UserName.Text)) { SPQuery query = new SPQuery(); query.Query = "<Where><Contains><FieldRef Name='UserName' /><Value Type='Text'>" + tb_UserName.Text + "</Value></Contains></Where>"; users = userList.GetItems(query); } else { users = userList.Items; } foreach (SPListItem user in users) { html.Append("<tr>"); html.Append("<td>" + user.Title); html.Append("</td>"); html.Append("<td>" + ConvertoString(user["UserName"])); html.Append("</td>"); html.Append("<td>" + ConvertoString(user["Gender"])); html.Append("</td>"); html.Append("<td>" + ConvertoString(user["Tell"])); html.Append("</td>"); html.Append("<td>" + ConvertoString(user["Adress"])); html.Append("</td>"); html.Append("<td>" + ConvertoString(user["ApprovalStatue"])); html.Append("</td>"); html.Append("<td>" + ConvertoString(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>"); } }
改
protected void bt_SaveClick(object sender, EventArgs e) { SPList userlist = SPContext.Current.Web.Lists["UserInfo"]; SPListItem user = userlist.GetItemById(Convert.ToInt32(Request.QueryString["ID"])); user["Title"] = this.tx_Title.Text; user["UserName"] = this.tx_UserName.Text; user["Gender"] = this.tx_Gender.Text; user["Tell"] = this.tx_tell.Text; user["Adress"] = this.tx_adress.Text; user.Update(); base.Response.Redirect("UserInfoList.aspx"); }