Highlighting and selecting an item in a DataList
2006-02-22 10:46
369 查看
http://www.codeproject.com/aspnet/HighlightSelectDataList.asp
This article describes how you can easily highlight and select an item in a DataList.
Providing visual feedback on which row the mouse is over.
Enabling the user to select a
I will show how this can be easily accomplished.
The Northwind database is available (the example uses the local SqlExpress connection with the default parameters).
The
The
After you have implemented the binding for the
By setting the
Once we have a button available, we know that when it's clicked, it will trigger the
The code is executed during the
The third attribute serves to simulate a click on the hidden '
In order to show info about the selected
The article can be found here: Steps to Write GridView PostBack Events.
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
DataListcontrol 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
DataListItemby 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
DataListwill be bound to the Northwind database and shows some info from the Employee table.
The
DataListuses '
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
ItemTemplateby adding a
LinkButtonas the first control, let's call it for a change '
SelectButton'. The documentation reads the
ItemCommandevent will be raised when any button of the
ItemTemplateis clicked. Since we don't want to use this
LinkButtondirectly for selecting a row, we are going to hide it. This can be done by adding a
styleattribute:
<ItemTemplate> <asp:LinkButton id="SelectButton" Text="Select" CommandName="Select" runat="server" style="display:none"/> ...
By setting the
displaystyle 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
ItemCommandevent. 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
ItemDataBoundevent. 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
GetPostBackEventReferencerefers to the first control in the item, the '
SelectButton', and will result, for example, in the following script added to the
OnClickevent of the rendered
SPANelement:
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; }
LastNameLabelis the name of the label in the
ItemTemplatethat 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 forDataGrids instead of
DataLists.
The article can be found here: Steps to Write GridView PostBack Events.
相关文章推荐
- [JS Array]Find an item in an array that contains a string and then return that string
- There is an obvious Error in npp src. ( About FORTRAN Syntax Highlighting And Fold )
- An Example of Pre-Query and Post-Query Triggers in Oracle Forms With Using Display_Item to Highlight Dynamically
- Get the content of an Iframe in Javascript – crossbrowser solution for both IE and Firefox
- Difference between menu item types; Display, Output and Action in Dynamics Ax
- Stopping and/or Restarting an embedded Jetty in...
- Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application (9 of 10)
- Create an avatar and chat with your friends in rooms you design
- Can we use function on left side of an expression in C and C++?
- the differences between DataGrid and DataList in HTML View
- How to get URL and QueryString value in an ASP.NET page
- Using GDI+ in a Single Document MFC Project to Rotate, Zoom, and Constraint an Image
- POST a string and an image in one request using HttpClient
- How to enable syntax highlighting and other options in VIM
- About UID and How to autostart an application on boot up in 3rd- Startup List Management API
- You should be the top dog in your department and always keep an eye on the outside world.
- Dr. An Wang and Wang Laboratories (An legend in IT history)
- Cannot have both DTEND and DURATION in an event
- Flex lets you define item renderers and item editors in three different ways:
- Populate A List Item With Record Group In Oracle Forms Using Populate_List And Create_Group_From_Query Command