Ayende vs GridView: Part 2
Okay, here is a simple scenario, I need to add a couple of buttons to the end of each row on the GridView. How do I do that?
Well, I started out by adding them by override the OnDataBound () method and adding the buttons as ButtonField. It works, it's simple, but it's not done. I wanted to add a javascript confirm to one of the buttons, since it's a destructive change. How do I do that? No idea, and I don't see it being possible. I took a look using Reflector, and it's generating the button internally, and there is no way I could see to plug into that and pass the javascript that I wanted to add.
Then I looked at TemplateField, which looked like it could do what I wanted, but I wanted to do it via code, not via XML. And I couldn't find a single reference on how to do that. In the end, what I did was to take a sample of the XML code, compile it, and then look into "%WINDIR%\Microsoft.Net\Framework\v2.50727\Temporary ASP.NET Files\" to see what the compiled end result of the pages looked like, and then see how it's done. Not fun, I promise you.
The end result looked like this:
dpublic void AddControlToGrid()
{
foreach(Control ctrl in controlsToAdd)
{
Control temp = ctrl;
TemplateField template = new TemplateField()
template.TemplateItem = new CompiledTemplareBulider(
delegate(Control parent) { ((IParserAccessor)parent).AddParsedSubObject(temp); }
);
this.Columns.Add(template);
}
}
I create the controls I want to add the usual way, adding javascript validation if I need it, and then I generate a TemplateField to wrap them for the GridView. This works, but I don't really like the way I'd to find it.
Comments
Comment preview