Click here to Skip to main content
Click here to Skip to main content

Highlighting and selecting an item in a DataList

, 14 Feb 2006
Rate this:
Please Sign up or sign in to vote.
This article describes how you can easily highlight and select an item in a DataList.

Introduction

ASP.NET server controls provide a rich environment to produce high quality web sites. Sometimes though, you just want to go that little bit further. DataLists offer a convenient way to render lists with data that can be fully customized using templates. I was last experimenting with a DataList control and I soon found two features that where not immediately available out of the box:

  • Providing visual feedback on which row the mouse is over.
  • Enabling the user to select a DataListItem by clicking anywhere on the item.

I will show how this can be easily accomplished.

Using the code

The example provided assumes the following things:

  • The Northwind database is available (the example uses the local SqlExpress connection with the default parameters).
  • The DataList will be bound to the Northwind database and shows some info from the Employee table.
  • The DataList uses 'Flow' as RepeatLayout (otherwise the proposed highlighting solution does not work).

After you have implemented the binding for the DataList, you need to modify the DataList ItemTemplate by adding a LinkButton as the first control, let's call it for a change 'SelectButton'. The documentation reads the ItemCommand event will be raised when any button of the ItemTemplate is clicked. Since we don't want to use this LinkButton directly for selecting a row, we are going to hide it. This can be done by adding a style attribute:

<ItemTemplate>
<asp:LinkButton id="SelectButton" 
           Text="Select" 
                CommandName="Select" 
                runat="server" 
                style="display:none"/>
...

By setting the display style attribute to none, we assure that the button will be rendered and is available in the DOM structure.

Once we have a button available, we know that when it's clicked, it will trigger the ItemCommand event. This can be achieved as follows:

protected void DataList1_ItemDataBound(object sender, 
                             DataListItemEventArgs e) 
{
     if (e.Item.ItemType == ListItemType.Item || 
         e.Item.ItemType == ListItemType.AlternatingItem)
     { 
         //Add eventhandlers for highlighting 
         //a DataListItem when the mouse hovers over it.
         e.Item.Attributes.Add("onmouseover", 
                "this.oldClass = this.className;" + 
                " this.className = 'EntryLineHover'"); 
         e.Item.Attributes.Add("onmouseout", 
                "this.className = this.oldClass;");
         //Add eventhandler for simulating 
         //a click on the 'SelectButton'
         e.Item.Attributes.Add("onclick", 
                this.Page.ClientScript.GetPostBackEventReference(
                e.Item.Controls[1], string.Empty));
     }
}

The code is executed during the ItemDataBound event. Here, we add new attributes to the rendered DataListItems. The first two attributes have to do with the highlighting of the row when the mouse hovers over it. We just switch between two CSS styles.

The third attribute serves to simulate a click on the hidden 'SelectButton'. The GetPostBackEventReference refers to the first control in the item, the 'SelectButton', and will result, for example, in the following script added to the OnClick event of the rendered SPAN element:

javascript:__doPostBack('DataList1$ctxxx$SelectButton','')

In order to show info about the selected DataListItem, you could use:

protected void DataList1_ItemCommand(object source, 
                         DataListCommandEventArgs e) 
{ 
    txtEmployeeId.Text = 
      ((Label)e.Item.FindControl("LastNameLabel")).Text; 
}

LastNameLabel is the name of the label in the ItemTemplate that holds the last name of the selected employee.

Acknowledgements

The basic idea of implementing highlight and selection comes from an article that deals with the same issues but then for DataGrids instead of DataLists.

The article can be found here: Steps to Write GridView PostBack Events.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

W. Jansoone

United States United States
No Biography provided

Comments and Discussions

 
Generalthank you PinmemberMember 1096947824-Jul-14 11:23 
GeneralHighlight a particular row in a gridview on MouseOver PinmemberBinu198517-Jun-14 2:01 
GeneralMy vote of 4 Pinmemberiman khalil24-Sep-12 7:20 
Questioni need u help PinmembermavicocuQ16-Jul-12 23:06 
GeneralMy vote of 1 Pinmemberaasheeshh29-Dec-09 19:21 
GeneralGridView similare way [modified] Pinmemberzitun4-Apr-07 22:48 
GeneralRe: GridView similare way PinmemberJD814-May-08 17:34 
GeneralRe: GridView similare way Pinmemberashutosh kumar jha9-Feb-10 7:17 
QuestionHow to remove highlighting when DataList is refreshed? Pinmemberspin2k22-Jun-06 12:37 
GeneralGridview CSS Class hover with Javascript Pinmemberdrkwtkns27-Apr-06 6:06 
AnswerRe: Gridview CSS Class hover with Javascript PinmemberZaz9996-Aug-07 1:52 
GeneralHighlighting and selecting an item in a DataList Example Pinmembergrigorythegreat22-Feb-06 4:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 14 Feb 2006
Article Copyright 2006 by W. Jansoone
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid