C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » ASP.NET/Web Applications »

How to highlight a row in datalist when an user click on that row


Posted Date: 12 Apr 2005    Resource Type: Articles    Category: ASP.NET/Web Applications
Author: Parvathi GandhiMember Level: Silver    
Rating: 1 out of 5Points: 10



Some of our requirements will include things like while selecting a row in datalist that particular row has to be highlighted by specifying different colors to that row.

With the help of the <SelectedItemTemplate> we can acheive this.
Let us see one example for this.

HTML source:


<asp:datalist id="ItemsList" runat="server" CellSpacing="0" CellPadding="3" GridLines="Both" Width="280px">
<SelectedItemStyle BackColor="#AAAADD"></SelectedItemStyle>
<HeaderTemplate>
Employee Details
</HeaderTemplate>
<SelectedItemTemplate>
Employee Name:
<asp:Label id=lblName runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Employee Name") %>'>
</asp:Label><BR>
Qualification:
<asp:Label id=lblQualification runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Qualification") %>'>
</asp:Label><BR>
Profession:
<asp:Label id=lblProf runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Profession") %>'>
</asp:Label>
</SelectedItemTemplate>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<ItemTemplate>
<asp:LinkButton id="SelectButton" runat="server" CommandName="Select" Text="Select">Details</asp:LinkButton>
<%# DataBinder.Eval(Container.DataItem, "Employee Name") %>
</ItemTemplate> </asp:datalist>


Code for code behind:


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
ItemsList.DataSource = CreateDataSource()
ItemsList.DataBind()
End If
End Sub


Following is the code for the user defined function CreateDataSource.


Function CreateDataSource()

Dim dt As DataTable
Dim dr As DataRow
Dim i As Integer

'create a DataTable
dt = New DataTable

dt.Columns.Add(New DataColumn("Employee Name", GetType(String)))
dt.Columns.Add(New DataColumn("Qualification", GetType(String)))
dt.Columns.Add(New DataColumn("Profession", GetType(String)))

'Add some records in the datagrid

dr = dt.NewRow()
dr(0) = "Mini"
dr(1) = "M.C.A"
dr(2) = "System Analyst"
dt.Rows.Add(dr)

dr = dt.NewRow()
dr(0) = "Bini"
dr(1) = "B.E"
dr(2) = "Software Engineer"
dt.Rows.Add(dr)

dr = dt.NewRow()
dr(0) = "Gini"
dr(1) = "M.C.A"
dr(2) = "Project Leader"
dt.Rows.Add(dr)

'create a dataview and assign the datatable to it
Dim dv As DataView = New DataView(dt)
Return dv

End Function


write the following code in the itemcommand event of the datalist.


Private Sub ItemsList_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles ItemsList.ItemCommand

' Set the SelectedIndex property to select an item in the DataList.
ItemsList.SelectedIndex = e.Item.ItemIndex

' Rebind the data source to the DataList to refresh the control.
ItemsList.DataSource = CreateDataSource()
ItemsList.DataBind()
End Sub


Kindly check with this.

Parvathi.K



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Mobile Web Application
Previous Resource: How to make all columns Editable in a Datagrid at runtime ?
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET/Web Applications


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use