Cipher
Cipher

Reputation: 6102

Unhighlight the previous link button when the subsequent is clicked in DataList

I am highlighting a certain LinkButton within my DataList when the linkbutton is pressed. Each row has a link button, and when the user presses the next link button from any of the rows, the previous highlighted link button should get the normal transparent background.

For highlighting, I am using the following:

protected void DataList_ItemCommand(object source, DataListCommandEventArgs e)
     {
         if (e.CommandName == "select")
         {
             LinkButton highlighted = ((LinkButton)(e.Item.FindControl("Item")));
             highlighted.BackColor = System.Drawing.Color.Yellow;
...
}

How do I 'un-highlight' the previous link button, when the user choose any other link button from the data list? Since the post back are taking place, I can't have a global LinkButton variable to check the next time to store the previously clicked LinkedButton.

Any suggestions?

Upvotes: 0

Views: 608

Answers (1)

jmaglio
jmaglio

Reputation: 776

If you are rebinding the grid on page load, the color of the LinkButtons should be reset. However, something like this should be able to set the background color of all linkbuttons to transparent:

VB.NET:

For Each DLItem As DataListItem In DataList1.Items
  Dim unHighLight As LinkButton = DLItem.FindControl("Item")
       If Not unHighLight Is Nothing Then
          unHighLight.BackColor = System.Drawing.Color.Transparent
       End If
Next

Dim highlighted As LinkButton = e.Item.FindControl("item")
highlighted.BackColor = System.Drawing.Color.Yellow

C#:

foreach (DataListItem DLItem in DataList1.Items)
{
   //unhighlight all ilnkbuttons
   LinkButton unHighLight = ((LinkButton)(DLItem.FindControl("Item")));
   if (unHighLight != null)
   {
    unHighLight.BackColor = System.Drawing.Color.Transparent;
   }
 }

LinkButton highlighted = ((LinkButton)(e.Item.FindControl("Item")));
highlighted.BackColor = System.Drawing.Color.Yellow;

Upvotes: 1

Related Questions