A few days ago, I decided to try to extend the Repeater Control, by adding an OnItemCommand, like the System.Web.UI.WebControl. Simple I thought. Shouldnt be that hard I thought.
I couldnt have been more wrong.
I have learned alot with the whole experience. I have learned about event bubbling, the way that events get passed up the control tree till they are handled by their parents. Iv learned about the way that items that are nested will need to be databound for the event to be fired, even if the data is being shown on the page, you still have to explicitly call DataBind(); And I got some help from a random guy (who works for telerik!) on StackOverflow, thanks!
Basically, this is the way it works. When a user hits a button (Like a <asp:button) on the page, it will bubble up (OnBubbleEvent) on the ITemplate (In this case, the actual type of the ItemRepeater). That will do its magic, work out if its a CommandEventArgs, if it is, then bubble it up:
protected override bool OnBubbleEvent(object source, EventArgs e)
{
if (e is CommandEventArgs)
{
RepeaterCommandEventArgs args = new RepeaterCommandEventArgs(this, source, (CommandEventArgs) e);
base.RaiseBubbleEvent(this, args);
return true;
}
return false;
}
Taken from System.Web.UI.WebControls.RepeaterItem.OnBubbleEvent(object source, EventArgs e);
From there, the Repeater will intercept that, then process it:
private static readonly object EventItemCommand = new object();
protected override bool OnBubbleEvent(object sender, EventArgs e)
{
bool flag = false;
if (e is RepeaterCommandEventArgs)
{
this.OnItemCommand((RepeaterCommandEventArgs)e);
flag = true;
}
return flag;
}
protected virtual void OnItemCommand(RepeaterCommandEventArgs e)
{
RepeaterCommandEventHandler handler = (RepeaterCommandEventHandler)base.Events[EventItemCommand];
if (handler != null)
{
handler(this, e);
}
}
public event RepeaterCommandEventHandler ItemCommand
{
add
{
base.Events.AddHandler(EventItemCommand, value);
}
remove
{
base.Events.RemoveHandler(EventItemCommand, value);
}
}
This is where the fun starts. First off, in the RepeaterItem source code, you cannot call:
RepeaterCommandEventArgs args = new RepeaterCommandEventArgs(<strong>this</strong>, source, (CommandEventArgs) e);
base.RaiseBubbleEvent(this, args);
because this refers to a RepeaterItem, which, in my case, it is not because its a Nettiers generated RepeaterItem. So you have to go off and create your own CommandEventArgs. If you create your own EventArgs, the event handler RepeaterCommandEventHandler wont work, because its not of the type RepeaterCommandEventArgs. Which is a pain in the arse!
Once you have created the EventArgs and the event handler though, its fairly simple from there – you just modify the Reflector’ed code (From the RepeaterControl), to get it to handle your custom Repeater Item’s event, and not the RepeateItem default event which you cant even call!
But your not done! I found out that the command wont get called when someone clicky’s on it, because you havnt called DataBind(); on the control, which explains some … oddities I spotted in the code when I was digging around inside the Repeater – I kept finding calls to “EnsureDataBound()” – which is called OnPreRender:

Note: I will provide code samples and Nettiers templates very soon, as soon as I have integrated my changes into Nettiers!
Recent Comments