So today I needed to do something I had done before.  Simple, right?  Just copy/paste the code from another project and <wham> everything works, right?  NOT!
Today’s goal was to make a popup edit window for the contents of a specific grid row.  Each gridview row has it’s own edit button.  I want the user to be able to edit the controls of the gridview row within a nifty popup window.
First we have to add a Panel to our HTML.  This panel will contain everything included in our popup window.  Here’s a good start:
Notice the object ID names here.  They are critical and must match exactly with how you specify them in the ModalPopupExtender.  If you get just one wrong, the popup won’t fire and you may not even see an error depending on how your browser is configured.
Also notice at the end of the panel is a save button with an OnClick event, but the Cancel button does not have one.  This is intentional.  Unless you have need to extra functionality within the Cancel operation, you can hook the ‘cancel’ functionality of the MPE directly to the button.  Alternatively you can specify an OnClick event for the Cancel button and specifically issue the MPE.Hide() command.  It’s your choice.
Once your Panel is in place, now it’s time to build the ModalPopupExtender itself.  Here how this one will look:
There’s a lot here, but basically it all makes sense.  We give the MPE an ID for itself.  But notice the TargetControlId is set to an object we haven’t added yet.  That object, btn_Hidden1 is the trick.  Normally an MPE requires being hooked into a working button.  That’s what we’re doing here, we’ve just made the button invisible.
By setting an MPE to a button that isn’t used, it gives you the ability to create an MPE and manually manipulate it elsewhere in the code-behind.  Here’s the code for the hidden button, it’s quite simple:
Note how I’ve used a style with display: none instead of the usual Visible=”false”.  Why?  Because a Visible=”false” will not render the control at all on the client side, thus the MPE cannot find it’s button to hook.  The MPE will fail to fire the popup and it’s doubtful you’ll even see an error.  So do not use the Visible=, instead use a style with display: none on your hidden button.
We have one last thing to build on the HTML side:  Our gridview command button.  This button renders for each row in the Gridview control.  It’s responsible for opening the actual popup window when it’s clicked.
Adding the button is easily done.  Simply add the button to a template field in your gridview row like such:
In this particular case, we’re using an icon image to represent the button but you get the point nonetheless.
The key here is we’re using the gridview RowCommand event to handle the popup action.  So the button must have the CommandName and CommandArgument parameters specified.
Finally we’re done with the HTML side.  Now let’s build the code-behind to drive this popup.
For your Gridview control, make sure you’ve specified a function for the RowCommand event like so:
Now build the code-behind to drive the ModalPopupExtender and render the popup.  Here’s the final piece of the puzzle:
See how it’s done?  Simply call the .Show() method of the ModalPopupExtender object.  The browser client handles the rest automatically.
Well if you’ve read this far, then you’ve probably realized by now that I never answered the question of “Why didn’t it work when you copy/paste’d the code from another project?”  I made the mistake of using Visible=”False” on the hidden button instead of using style=”display: none” </thumpsforehead>