事件触发时显示警报消息

时间:2021-08-30 00:02:43

I have the following requirement. I have stored a list of dictionary items. I want Dictionary Key to be unique. I have written the following code like whenever any new item is saving with existing key name, it should throw an alert like "Item already exist".

我有以下要求。我已经存储了一个字典项列表。我希望Dictionary Key是唯一的。我编写了以下代码,就像任何新项目使用现有密钥名称保存一样,它应该抛出类似“项已存在”的警报。

What I am doing is like comparing the Key value with existing dictionary keys while saving the item. I have written the code in the ItemSaving event.

我正在做的是在保存项目时将Key值与现有字典键进行比较。我已经在ItemSaving事件中编写了代码。

 public class IsItemExist
    {
        Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

        public void OnItemSaving(object sender, EventArgs args)
        {

            Item dbItem = master.GetItem("/sitecore/content/Administration/Development Settings/Lookups");
            Item selectedItem = Event.ExtractParameter(args, 0) as Item;
            foreach (Item item in dbItem.Axes.GetDescendants())
            {
                if (item.Template.Name.Contains("entry"))
                {
                    if (item.Fields["Key"].Value == selectedItem.Fields["Key"].Value)
                    {
                        Sitecore.Context.ClientPage.ClientResponse.Alert("Item is already exist");
                    }
                }
            }

        }


    }

web.config entry

<event name="item:saving">
<handler type="CustomEvent.IsItemExist, CustomEvent" method="OnItemSaving"/>
</event>

It's showing alert message and i am able to save the item. 1.I don't want to save the Item with duplicate value again. 2.I am getting 2 alert messges when click on the save button.why?

它显示警报消息,我可以保存该项目。 1.我不想再次使用重复值保存项目。 2.点击保存按钮后,我得到2个警告信息。为什么?

any help will be appreciated.Thanks Guys..

任何帮助将不胜感激。谢谢各位...

1 个解决方案

#1


6  

You should add your handler to item:adding event and set the Cancel property of the event result to true:

您应该将处理程序添加到item:添加事件并将事件结果的Cancel属性设置为true:

  <event name="item:adding">
    <handler type="CustomEvent.DoesItemExist, CustomEvent" method="OnItemAdding" />
  </event>
public class DoesItemExist
{
    Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

    public void OnItemAdding(object sender, EventArgs args)
    {
        Item dbItem = master.GetItem("/sitecore/content/Administration/Development Settings/Lookups");
        Item selectedItem = Event.ExtractParameter(args, 0) as Item;
        foreach (Item item in dbItem.Axes.GetDescendants())
        {
            if (item.Template.Name.Contains("entry"))
            {
                if (item.Fields["Key"].Value == selectedItem.Fields["Key"].Value)
                {
                    SitecoreEventArgs evt = args as SitecoreEventArgs;
                    evt.Result.Cancel = true;
                    Sitecore.Context.ClientPage.ClientResponse.Alert("Item already exists");
                }
            }
        }
    }
}

#1


6  

You should add your handler to item:adding event and set the Cancel property of the event result to true:

您应该将处理程序添加到item:添加事件并将事件结果的Cancel属性设置为true:

  <event name="item:adding">
    <handler type="CustomEvent.DoesItemExist, CustomEvent" method="OnItemAdding" />
  </event>
public class DoesItemExist
{
    Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master");

    public void OnItemAdding(object sender, EventArgs args)
    {
        Item dbItem = master.GetItem("/sitecore/content/Administration/Development Settings/Lookups");
        Item selectedItem = Event.ExtractParameter(args, 0) as Item;
        foreach (Item item in dbItem.Axes.GetDescendants())
        {
            if (item.Template.Name.Contains("entry"))
            {
                if (item.Fields["Key"].Value == selectedItem.Fields["Key"].Value)
                {
                    SitecoreEventArgs evt = args as SitecoreEventArgs;
                    evt.Result.Cancel = true;
                    Sitecore.Context.ClientPage.ClientResponse.Alert("Item already exists");
                }
            }
        }
    }
}