sharepoint 列表的基本操作

moss 列表的基本操作,以下是对列表中字段的数据进行简单的添、删、改、查。

1)前台代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ListOperate._Default" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        学号<asp:TextBox ID="txtId" runat="server" Width="120px"></asp:TextBox><br />
        姓名<asp:TextBox ID="txtName" runat="server" Width="120px"></asp:TextBox><br />
        学校<asp:DropDownList ID="dropSchool" runat="server" Width="128px">
        </asp:DropDownList>
        <br />
        <asp:Button ID="btnAdd" runat="server" Text="添加" onclick="btnAdd_Click" />
        <asp:Label ID="lblMessage" runat="server"></asp:Label><br />
        标示<asp:Label ID="lblID" runat="server"></asp:Label><br />
        学号<asp:TextBox ID="txtUpdateId" runat="server" Width="120px"></asp:TextBox><br />
        姓名<asp:TextBox ID="txtUpdateName" runat="server" Width="120px"></asp:TextBox><br />
        学校<asp:DropDownList ID="dropUpdateSchool" runat="server" Width="128px"
            ></asp:DropDownList>
        <br />
        <asp:Button ID="btnUpdate" runat="server" Text="修改" onclick="btnUpdate_Click"/>
        <asp:Label ID="lblUpdateMessage" runat="server"></asp:Label>
        <table>
        <asp:Repeater ID="rptStudent" runat="server"
            onitemcommand="rptStudent_ItemCommand">
        <HeaderTemplate>
        <tr>
            <td>标示</td>
            <td>学号</td>
            <td>姓名</td>
            <td>学校</td>
        </tr>
        </HeaderTemplate>
        <ItemTemplate>
        <tr>
            <td><%#Eval("ID") %></td>
            <td>
                <asp:LinkButton ID="lbtnId" runat="server" CommandName="get" CommandArgument='<%#Eval("ID") %>'><%#Eval("Title") %></asp:LinkButton>
            </td>
            <td><%#Eval("Name") %></td>
            <td><%#Eval("School") %></td>
            <td>
                <asp:LinkButton ID="lbtnDelete" runat="server" CommandName="delete" CommandArgument='<%#Eval("ID") %>'>删除</asp:LinkButton>
            </td>
        </tr>
        </ItemTemplate>
        </asp:Repeater>
    </table>
    </div>

    </form>
</body>
</html>


2)后台代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Data;

namespace ListOperate
{
    public partial class _Default : System.Web.UI.Page
    {
        string url = "http://ds01:6020/";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindListStudent();//绑定student列表
                BindListSchool(dropSchool); //绑定school列表
                BindListSchool(dropUpdateSchool);//绑定school列表
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string id = txtId.Text.Trim();//学号
            string name = txtName.Text.Trim();//姓名
            int schoolId = Convert.ToInt32(dropSchool.SelectedValue);//学校编号
            AddStudent(id, name, schoolId);//添加学生
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int ID = Convert.ToInt32(lblID.Text);//标示
            string id = txtUpdateId.Text.Trim();//学号
            string name = txtUpdateName.Text.Trim();//姓名
            int schoolId = Convert.ToInt32(dropUpdateSchool.SelectedValue);//学校编号
            UpdateStudent(ID, id, name, schoolId);//修改学生
        }
        protected void rptStudent_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                int id=Convert .ToInt32(e.CommandArgument.ToString());
                DelStudent(id); //删除学生
                BindListStudent();
            }
            if (e.CommandName == "get")
            {
                int id=Convert .ToInt32(e.CommandArgument.ToString());
                SPListItem sPListItem = GetStudentByID(id);
                lblID.Text=sPListItem.ID.ToString();
                txtUpdateId.Text = sPListItem["Title"].ToString();
                txtUpdateName.Text = sPListItem["Name"].ToString();
                dropUpdateSchool.SelectedValue = sPListItem["School"].ToString().Split(';')[0];
            }
        }
        //根据ID获取学生信息
        private SPListItem GetStudentByID(int id)
        {
            SPListItem sPListItem = null;
            using (SPWeb sPWeb = new SPSite(url).OpenWeb())
            {
                //SPQuery query = new SPQuery();
                //query.Query = "<Where>" +
                //                        "<Eq>" +
                //                            "<FieldRef Name='ID'/>" +
                //                            "<Value Type='Counter'>" + id + "</Value>" +
                //                        "</Eq>" +
                //                      "</Where>";
                SPList sPList = sPWeb.Lists["Student"];
                //sPListItem = sPList.GetItems(query)[0];
                sPListItem = sPList.GetItemById(id);
            }
            return sPListItem;
        }
        //删除学生
        private void DelStudent(int id)
        {
            using (SPWeb sPWeb = new SPSite(url).OpenWeb())
            {
                sPWeb.AllowUnsafeUpdates = true;
                SPList sPList = sPWeb.Lists["Student"];
                try
                {
                    sPList.Items.DeleteItemById(id);
                }
                catch (Exception ex)
                {
                    lblMessage.Text = ex.ToString();
                }
            }
        }
        //添加学生
        private void AddStudent(string id,string name,int schoolId)
        {
            using (SPWeb sPWeb = new SPSite(url).OpenWeb())
            {
                sPWeb.AllowUnsafeUpdates = true;
                SPList sPList = sPWeb.Lists["Student"];
                SPListItem sPListItem = sPList.Items.Add();
                sPListItem["Title"] = id;
                sPListItem["Name"] = name;
                sPListItem["School"] = schoolId;
                try
                {
                    sPListItem.Update();
                }
                catch (Exception ex)
                {
                    lblMessage.Text = ex.ToString();
                }
            }
            BindListStudent();
        }
        //修改学生
        private void UpdateStudent(int ID, string id, string name, int schoolId)
        {
            using (SPWeb sPWeb = new SPSite(url).OpenWeb())
            {
                sPWeb.AllowUnsafeUpdates = true;
                SPList sPList = sPWeb.Lists["Student"];
                SPListItem sPListItem = sPList.GetItemById(ID);
                sPListItem["Title"] = id;
                sPListItem["Name"] = name;
                sPListItem["School"] = schoolId;
                try
                {
                    sPListItem.Update();
                }
                catch (Exception ex)
                {
                    lblMessage.Text = ex.ToString();
                }
            }
            BindListStudent();
        }
        //绑定school列表
        private void BindListSchool(DropDownList dropItem)
        {
            dropItem.DataSource = GetListSchool();
            dropItem.DataTextField = "Title";
            dropItem.DataValueField = "ID";
            dropItem.DataBind();
        }
        //获取school列表
        private DataTable GetListSchool()
        {
            DataTable dtSchool = null;
            using (SPWeb sPWeb = new SPSite(url).OpenWeb())
            {
                SPList sPList = sPWeb.Lists["School"];
                dtSchool = sPList.Items.GetDataTable();
            }
            return dtSchool;
        }
        //绑定student列表
        private void BindListStudent()
        {
            rptStudent.DataSource = GetListStudent();
            rptStudent.DataBind();
        }
        //获取student列表
        private DataTable GetListStudent()
        {
            DataTable dtSudent = null;
            using (SPWeb sPWeb = new SPSite(url).OpenWeb())
            {
                SPList sPList = sPWeb.Lists["Student"];
               dtSudent = sPList.Items.GetDataTable();
            }
            return dtSudent;
        }

    }
}

 

你可能感兴趣的:(exception,server,String,asp,SharePoint,textbox)