Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Full-Row Selection in a DataGrid


Full-Row Selection in a DataGrid

The DataGrid control has supported row selection since its debut with ASP.NET 1.0. However, in order to select a row and get a corresponding server-side event you have to add a made-to-measure column to the DataGrid. The column is a normal button column with a special command name—Select.

<asp:ButtonColumn runat=server commandname=select ...><br>:<br></asp:ButtonColumn>

You normally place this column as the first or the last in the grid. The text displayed through the column can be anything you prefer. It can be a constant string like “Details” or “Select” as well as an image. In this case, you just set the Text property of the column with the proper HTML markup code:

< asp:ButtonColumn text="< img src=select.gif>" ...>

Alternately, the column can also be bound to a data-source column.

Why should you select a grid row? Typically, you click on a row to drill down into the information it represents. For example, if the grid lists your customers, you can click to see the details of the specified customer.

The key point is that you need a button column to enable row selection. Any command column is fine, but if you use one with the CommandName property set to “Select,” you get some extra benefits from the DataGrid control. For example, the grid will automatically apply the styles set in the SelectedItemStyle property and fire the SelectedIndexChanged event. Can you ask for more? Actually, many users will, sooner or later.

If you haven’t got it already, be prepared—it can be as soon as tomorrow or next week. If you provide users with a selectable DataGrid, they tend to ask for full-row selection, which is the capability of clicking anywhere in the row and having the page post back as if you clicked on the regular select column.

To implement that, you add a bit of script code to each row that detects clicking and posts back. However, what you need here is not simply a postback. You actually need a postback that brings in the reference to the clicked row. The simplest way to get this is stealing some code from the select column without removing it altogether. In the end, you have a DataGrid with a select column and the additional and unique capability of posting back no matter where you click.

You add an ItemDataBound event handler to your grid, defined as follows

:
LinkButton button;
button = (LinkButton) e.Item.Cells[0].Controls[0];
string js =GetPostBackClientHyperlink(button, "");
e.Item.Attributes["onclick"] = js;

The code above must run only if the item type is Item or AlternatingItem. You first retrieve a reference to the link (or push) button in the Select column. In the snippet above, I'm assuming it is the first control on the first column (this can be anything else in your real code). Next, you retrieve the client script code associated with the postback of the specified control. In other words, the GetPostBackClientHyperlink method returns the same Javascript call that executes when you click on the Select column. You bind this call to the onclick event of the row (the table's tag) and you're done.

If you really don't like having a Select command column, you can always give it a width of 0 or an empty text or a neutral bitmap.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Network and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at [email protected].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.