- Getting Started with Rendered Custom Controls
- Rendering Output
- Managing State
- Adding Event Infrastructure
- Using Your Control
- Summary
Adding Event Infrastructure
In addition to data, you must manually handle post back events. You can do this by implementing the IPostBackEventHandler interface, which lets ASP.NET call the RaisePostBackEvent method during control-event processing. Listing 4 shows how to implement IPostBackEventHandler:
Listing 4 Implementing IPostBackEventHandler
public void RaisePostBackEvent(string eventArg) { if (eventArg == "VoteButton") { OnClick(EventArgs.Empty); } }
The RaisePostBackEvent method in Listing 4 checks eventArg to see whether it's from the button that we added to this control in the Render method. In the onclick attribute in Listing 2, the second parameter is a call to the Page.GetPostBackEventReference method. The second parameter to Page.GetPostBackEventReference is "VoteButton", which is the same value checked in the RaisePostBackEvent in Listing 4.
When the button is rendered, the Page.GetPostBackEventReference method translates to JavaScript on the page. The "VoteButton" string is given to the JavaScript, which generates a post back when the button is clicked and sends this string back as the parameter of the RaisePostBackEvent.