In the previous post, I mentioned how annoying it is to have to treat an ImageButton separate from a Button and separate from a LinkButton when they are all essentially the same thing.
The answer here lies in the IButtonControl interface, (whose name strongly suggests that it should be a direct parent, not an interface, but oh well)…
Here’s how you’d add javascript and tool tips to those CommandField button controls, without having to write specific versions for all three:
1 protected void gvCrews_RowDataBound(object sender, GridViewRowEventArgs e)
2 {
3 //grab last column (the command column)
4 int cmdCol = gvCrews.Columns.Count - 1;
5 foreach (Control ctrl in e.Row.Cells[cmdCol].Controls)
6 {
7
8 IButtonControl btn = ctrl as IButtonControl;
9
10 if (btn != null)
11 {
12
13 if (btn.CommandName == "Delete")
14 {
15 ((WebControl)ctrl).Attributes.Add("onClick", "return confirm('Are you sure you want to delete the crew and its ENTIRE history? This cannot be undone...');");
16 ((WebControl)ctrl).ToolTip = "Click here to delete";
17 }
18 else if (btn.CommandName == "Edit")
19 {
20 ((WebControl)ctrl).ToolTip = "Click here to edit";
21 }
22 }
23 }
24 }
This way I can still change the look & feel of the GridView CommandField and I don’t have to recompile to support it. Unfortunately the IButtonInterface doesn’t allow for javascript attributes or tooltips, so I have to cast the control as a WebControl to add those in. Setting CausesValidation=true still causes an exception for the ImageButton though, so I haven’t figured out a way around this yet.
Now playing: Nine Inch Nails - The Hand That Feeds
I'm Ben Scheirman. I am a .NET software developer with a strong interest in agility. I work as a Principal Consultant with Sogeti.
Read more here.
email me
Ads by The Lounge
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.