Head.SmackOnTable();

Contains Nuts.

Archive for November, 2008

Extending Nettiers Part 2 – Adding an OnItemCommand

without comments

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!

Written by Monty

November 21st, 2008 at 8:10 pm

I am quite worried about the standard of coding these days

without comments

Ok, this is going to make me sound like a really old fart, which im not! Honest!

I was reading the blog of a Microsoft developer, someone whos team owns the Framework Design Guidelines, someone who worked on the VS Code Analysis team, and worked on FxCop.

Sample from davesbox.com

He then later states:

Comment from DavesBox.com

Im sorry, but WHAT THE F**K? Your not a fan of defensive coding, but you want us to do it in framework classes? With your code sample above, if label is null, guess what happens to your code? You may not like defensive coding, so you are going to assume that all of your code samples will work?

SetLabelTextIfNotEmpty(null);
SetLabelTextIfNotEmpty(Label1);  // No instance

Please tell me how your code will react to this? I guarentee that it will throw a null reference exception, because, well im passing in null to your method!

ALWAYS CHECK FOR NULL!

Im sure this is a minor lapse on your part, Dave, and im sure your code does not usually reflect this – if we had someone on my team, coding like this, I would either insist they change the way they code, or I would shift them off my team. Not checking for simple things, like null, is a major lapse in coding. You simply cannot guarentee that it will not be null, and wishfull thinking and happy thoughts arnt going to change it.

Written by Monty

November 18th, 2008 at 1:51 pm

Evolution of looping

without comments

While im writing a new project using vs.net 2008, Resharper cropped up a nice helper, saying I should use a lambda expression on my code, which made me think about how looping has evolved…


for (int i = 0; i < all.Count; i++)
{
tagCount.Add(new TagCount(container[i].Count, container[i]));
}

foreach (Tag tag in container)
{
tagCount.Add(new TagCount(tag.Count, tag));
}

container.ForEach(delegate(Tag tag)
{
tagCount.Add(new TagCount(container.Count, tag));
});

container.ForEach(tag => tagCount.
Add(new TagCount(container.Count, tag)));

Iv always thought lambda’s were gimmicky, but I do have to admit, they do look fancy and makes things like what im doing easier.

Written by Monty

November 17th, 2008 at 4:36 pm

Posted in .NET, Misc

Tagged with , ,

Introduction to CSS Adapters – Label Control

without comments

CSS Adapters in .net are rarely used, even though they are really powerful and can fix things like xhtml validation errors. This very quick sample will show you how to replace the <span> tags with a <div>

Create a class file, something like “LabelOverride.cs”, and put in the following:


using System.Web.UI;

namespace CSSAdapterTest
{
public class LabelOverride : System.Web.UI.WebControls.Adapters.WebControlAdapter
{
protected override void RenderBeginTag(HtmlTextWriter writer)
{
writer.RenderBeginTag(HtmlTextWriterTag.Div);
}
protected override void RenderEndTag(HtmlTextWriter writer)
{
writer.RenderEndTag();
}
}
}

Create a folder in the web application called “App_Browsers”, and create a .browsers file called “CSSAdapter.browser”, and enter in the following:


<browsers>
<browser refID="Default">
<controlAdapters>
<adapter controlType="System.Web.UI.WebControls.Label"
adapterType="CSSAdapterTest.LabelOverride" />
</controlAdapters>
</browser>
</browsers>

This basically tells asp.net to override the controlType’s renderer with the one you have specified.

Once you have done this, and when you build your application, all <span>’s will disappear and be replaced with <div>’s!

Written by Monty

November 5th, 2008 at 1:53 pm