添加项目的事件接收器被多次触发

时间:2016-09-20 07:48:09

标签: c# sharepoint-2013

这是我的事件接收者的代码:

    public override void ItemAdded(SPItemEventProperties properties)
    {            
            base.ItemAdded(properties);
            SPSite site = new SPSite("http://bp1amsapt283:2459/sites/TestSite");
            SPWeb web = site.OpenWeb();
            SPList list = web.Lists["CollegeList"];

            try
            {                   
                    SPList list1 = web.Lists["DestinationList"];
                    SPListItem item1 = list1.Items.Add();
                    if (list.ItemCount > 0)
                    {
                        SPListItem item = list.Items[list.ItemCount - 1];

                        item1["Title"] = item["Title"];
                        item1["First Name"] = item["First Name"];
                        item1["Middle Name"] = item["Middle Name"];
                        item1["Last Name"] = item["Last Name"];
                        item1["Date Of Birth"] = item["Date Of Birth"];

                        var add = item["Address"];
                        if (add.ToString().Contains("<"))
                        {
                            //string add1;
                            string[] add1 = add.ToString().Split('>');
                            string[] word = add1[1].Split('<');
                            item["Address"] = word[0];
                        }

                        item1["Address"] = item["Address"];
                        item1["Gender"] = item["Gender"];
                        item1["Courses"] = item["Courses"];
                        item1["Branch"] = item["Branch"];
                        item1["Approver"] = item["Approver"];
                        this.EventFiringEnabled = false;

                        item1.Update();
                        this.EventFiringEnabled = false;
                    }
                    else
                    {
                        item1["Title"] = properties.AfterProperties["Title"];
                        item1["First Name"] = properties.AfterProperties["First Name"];
                        item1["Middle Name"] = properties.AfterProperties["Middle Name"];
                        item1["Last Name"] = properties.AfterProperties["Last Name"];
                        item1["Date Of Birth"] = properties.AfterProperties["Date Of Birth"];
                        item1["Address"] = properties.AfterProperties["Address"];
                        item1["Gender"] = properties.AfterProperties["Gender"];
                        item1["Courses"] = properties.AfterProperties["Courses"];
                        item1["Branch"] = properties.AfterProperties["Branch"];
                        item1["Approver"] = properties.AfterProperties["Approver"];
                    }
                }

            catch (Exception ex)
            {
                throw ex;
            }
        }

在调用事件之前,我有一个可视webpart,我将项目插入到列表中,并在该列表上调用此事件接收器以将项目添加到另一个列表中。该项目正在添加,但它为单个项目添加了两次。此外,对于每个项目,它首先显示优先添加的项目,然后显示当前项目两次。任何人都可以帮我解决。

这是我的可视化Web部件类文件::

的代码
public partial class VisualWebPart1UserControl : UserControl
{
    private static int iD = 0;
    private int myId = 0;

    public VisualWebPart1UserControl()
    {
        iD++;
        this.myId = iD;
    }


   public  int idcount()
   {
       return myId;
   }

    SPUser name;

    protected void Button1_Click(object sender, EventArgs e)
    {
                SPSite site = SPContext.Current.Site;
                SPWeb web = site.OpenWeb();
                SPList list = web.Lists["CollegeList"];
                SPListItem itemToAdd = list.Items.Add();
                itemToAdd["Title"] = idcount();
                itemToAdd["First Name"] = TextBox3.Text;
                if (TextBox7.Text != "Enter Middlename Here")
                    itemToAdd["Middle Name"] = TextBox7.Text;
                itemToAdd["Last Name"] = TextBox8.Text;
                itemToAdd["Date Of Birth"] = TextBox9.Text;
                itemToAdd["Address"] = TextBox10.Text.ToString();
               // Console.WriteLine(itemToAdd["Address"]);
                itemToAdd["Gender"] = RadioButtonList2.Text;
                itemToAdd["Courses"] = DropDownList2.SelectedValue;
                itemToAdd["Branch"] = DropDownList4.SelectedValue;
                itemToAdd["Approver"] = name;

                itemToAdd.Update();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Success", "javascript:alert('form saved successfully.');", true);

                ClearTextBoxes(this);           
    }

    private void ClearTextBoxes(Control control)
    {
        foreach (Control c in control.Controls)
        {
            if (c is TextBox)
            {
                ((TextBox)c).Text = "";
            }

            DropDownList2.SelectedValue = "BTech";
            DropDownList4.SelectedValue = "Mechanical";

            if (c is RadioButton)
            {
                ((RadioButton)c).Checked = false;
            }

            if(c is PeopleEditor)
            {
                ((PeopleEditor)c).Entities.Clear();
                ((PeopleEditor)c).ResolvedEntities.Clear();
            }
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        List<string> content = new List<string>();

        if (DropDownList2.SelectedValue == "BSc")
        {
            content.Add("Mathematics");
            content.Add("Biology");
            content.Add("Chemistry");

        }
        else if (DropDownList2.SelectedValue == "BTech")
        {
            content.Add("Mechanical");
            content.Add("Computer Science");
            content.Add("Electronics");
        }

        DropDownList4.DataSource = content;
        DropDownList4.DataBind();
        content.Clear();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        SPSite oSPsite = new SPSite(SPContext.Current.Site.ID);

        SPWeb oSPWeb = oSPsite.OpenWeb();
        SPList list1 = oSPWeb.Lists["PeoplePicker"];

        oSPWeb.AllowUnsafeUpdates = true;

        int iPeople = PeopleEditor1.ResolvedEntities.Count;

        SPListItem itemToAdd1 = list1.Items.Add();


        for (int i = 0; i < iPeople; i++)
        {
            PickerEntity peEntity = PeopleEditor1.ResolvedEntities[i] as PickerEntity;

            SPUser user = SPContext.Current.Web.EnsureUser(peEntity.Key);

            itemToAdd1["Approver"] = user;
            name = user;
            itemToAdd1.Update();
        }
    }

    protected void Unnamed4_Click(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(typeof(Page), "closePage", "window.close();", true);
    }
}

0 个答案:

没有答案