Creating Event receivers in VS 2010

You can add additional event receiver by: click the event receiver item, and look in the Property Pane. Locate the event and change False to True. VS 2010 will update the Element.xml and *.CS file accordingly.

We’re creating an event receivers: List Item Event Receiver, on Announcement. We will checks for the expiration date. If no expiration dtae is specified, then cancel the ItemAdding.

Create new project in VS 2010, add Evenr Reciever, select List Item Event receiver, Announcement, and check ItemAdding.

Add this code in the ItemAdding() :

About base.ItemAdding – Base class method ItemAdding should be called at the end of the event – but this is not the case if you might cancel it. So:
Always call the base class method at the end of your handler, except if you are trying to cancel the event.

Press F5 to debug. When you add an announcement and without giving an expiration date, you get an error message.

You can use RedirecURL to redirect user to a custom error page.

Add an Application to your project, name ir CustomError.aspx. Open the file in source code view, and drag a Label control into the Main area, and rename it lblErrormessage. In the Page_Load Event, enter these:

Go back to EvenReceiver1.cs, make the change about redirection – and then try again by pressing F5.

then you will see this is you didn’t enter expiration date:

Or, if you don’t want to use URL redirect, you can simply display the message in the dislog box:

Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: