Tuesday, November 29, 2005

Adding Javascript to ANY CommandField control

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

#    Comments [1] | Trackback
Tracked by:
"private health insurance" (private health insurance) [Trackback]
"auto insurance quotes" (auto insurance quotes) [Trackback]
"phentermine" (phentermine) [Trackback]
"ditech" (ditech) [Trackback]
"bad credit loan mortgage" (bad credit loan mortgage) [Trackback]
"credit card companies" (credit card companies) [Trackback]
"hotels orlando" (hotels orlando) [Trackback]
"cheap phentermine online" (cheap phentermine online) [Trackback]
"texas hold em odds" (texas hold em odds) [Trackback]
"free party poker" (free party poker) [Trackback]
"online casino reviews" (online casino reviews) [Trackback]
"free play casino" (free play casino) [Trackback]
"home owner insurance" (home owner insurance) [Trackback]
"balance transfer credit cards" (balance transfer credit cards) [Trackback]
"panama city hotels" (panama city hotels) [Trackback]
"online cialis" (online cialis) [Trackback]
"penis enlargement" (penis enlargement) [Trackback]
"online weight loss" (online weight loss) [Trackback]
"get credit report" (get credit report) [Trackback]
"equifax credit" (equifax credit) [Trackback]
"bad debt" (bad debt) [Trackback]
"online casinos" (online casinos) [Trackback]
"poker sites" (poker sites) [Trackback]
"dating sites" (dating sites) [Trackback]
"bingo" (bingo) [Trackback]
http://9lt-free-porn.info/49946855/index.html [Pingback]
http://9lo-free-porn.info/92061481/statistics-bad-effects-from-seeing-nude.html [Pingback]
http://9lm-free-porn.info/58810207/index.html [Pingback]
http://9lm-free-porn.info/25726031/index.html [Pingback]
http://9lr-free-porn.info/28844559/hot-mazda-girl.html [Pingback]
http://9lk-free-porn.info/88831182/index.html [Pingback]
http://9lr-free-porn.info/97892018/index.html [Pingback]
http://9lt-free-porn.info/44587489/spray-porn.html [Pingback]
http://9lp-free-porn.info/95264086/aunt-judy-mature-amatures.html [Pingback]
http://9lm-free-porn.info/14543920/index.html [Pingback]
http://9lt-free-porn.info/94771489/index.html [Pingback]
http://9lt-free-porn.info/66349201/index.html [Pingback]
Thursday, December 08, 2005 3:43:05 PM (Central Standard Time, UTC-06:00)
Hi Ben,

I was looking around for some way to include the javascript confirm for CommandField when I stumbled onto your entry here. Snapping up your example, I was pleased to see it easily it seemed to fit the job.

However, I hit a bit of a snag. As soon as I add the onclick attribute to the command button, the gridview no longer seems to fire the RowCommand event. If I remove that single attribute addition from the code it works again.

Did you happen to run into this?
Matt
Comments are closed.
Credit - Refinance - Personal Loans - Internet Marketing