views:

211

answers:

1

What's the best way to create a pop-up like modal window that shows upon clicking the Select button in GridView?

   <asp:CommandField ShowSelectButton="true" ButtonType="Button" SelectText="Select Me" />

Also, bonus if you know how to implement this via jQuery.

+2  A: 
  • Go into design mode and choose the ticker in the top-right corner of the gridview.
  • Select "Edit Columns" and choose your CommandField.
  • Under the properties (right hand side), select "Convert to TemplateField" and hit OK, and you'll have access to the LinkButton generated by the CommandField.
  • Add a property to the LinkButton for OnClientClick and specify your javascript function in there.

e.g.
<asp:LinkButton ID="LinkButton1" runat="server" OnClientClick="openModal();" />

If you don't want the page to postback, openModal() should return false

You can create a modal dialog easily with jQuery UI. The options and some examples are available in the docs: http://docs.jquery.com/UI/Dialog

If you only need something really simple, you can also use the jQuery simple modal plugin available here: http://www.ericmmartin.com/projects/simplemodal/ The examples from this plugin will give you a nice looking confirmation dialog, or any other informational window (even embedded iframes).

Jim Schubert
good solution; another might be the ajax control toolkit's modaldialog but the jqueryui dialog is better between the two...
Tahbaza
you could also just add a CssClass to the CommandField control, and then use jQuery to select the buttons with that class and assign a click event handler to it that opens your modal. I'd also use jQuery-UI's modal.
dave thieben
@Dave: that's a good solution, but you'll need to be careful with adding classes to columns with generated markup in GridViews, knowing that the jQuery selector will sometimes have to be written to select the cell's children anchors. For instance, in IE7 (or maybe IE-compatibility mode), if you add a class to columns and that class has a CSS `:hover` (e.g. for text-underline), the `:hover` will override the jQuery click event. I experienced this with jQuery 1.3.2.
Jim Schubert
Wow, thanks guys - yes I think I'm going to go with jQuery after all. Not a big fan of MS controls although they both abstract the underlying JavaScript, I would still prefer jQuery.
firedrawndagger