Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing a datalist item color on click, then revert back to original

Status
Not open for further replies.

belle9

Programmer
Nov 3, 2004
87
US
As the title states, I'm changing the forecolor of a linkbutton when it is clicked on. However, I'd like for the color to go back to its original once I click on another item, ie only the item clicked should be red.

Here's what I have:
Code:
<asp:datalist id="dlCategories" runat="server">
     <ItemTemplate>
         <P class="wht_14" style="MARGIN: 8px 18px 2px 0px" align="right"><b>
         <asp:LinkButton id="Linkbutton1" Runat="server" CommandArgument='<%# Container.DataItem("categoryID") %>' CommandName="Edit"><%#Container.DataItem("CategoryName")%></asp:LinkButton></b></P>
         <!-- Begin Product Listing -->
         <asp:Panel id="PanelProd" runat="server" Visible="false">
              <asp:DataList ID="dlProd" Runat="server">
                   <ItemTemplate>
                   <P class="wht_11" style="MARGIN: 0px 26px 4px 0px" align="right">
                       <asp:Image ID="imgArrow" Runat="server" Visible="False" ImageUrl="/images/ico_arrowRight.gif"></asp:Image>
                    [b]  <asp:LinkButton id="linkButton2" Runat="server" OnClick="ProductDetailClicked" CommandArgument='<%# Container.DataItem("productsID") %>'><%#Container.DataItem("ProductName")%></asp:LinkButton></P>
                 [/b]  </ItemTemplate>
              </asp:DataList>
         </asp:Panel>
         <!-- End Product Listing -->
     </ItemTemplate>
</asp:datalist>

Code to change the color:
Code:
Public Sub ProductDetailClicked(ByVal sender As Object, ByVal e As EventArgs)

      
        PanelMainProducts.Visible = False
        PanelProduct.Visible = True
        myConnection = New SqlConnection(conString)
        myConnection.Open()

        mycommand = New SqlCommand("getSpecificProduct", myConnection)
        mycommand.CommandType = CommandType.StoredProcedure

        Dim objButton As LinkButton

        If Session("ProdID") = "" Then
            objButton = CType(sender, LinkButton)
            mycommand.Parameters.Add(New SqlParameter("@productsID", SqlDbType.Int))
            mycommand.Parameters("@productsID").Value = objButton.CommandArgument
        Else
            mycommand.Parameters.Add(New SqlParameter("@productsID", SqlDbType.Int))
            mycommand.Parameters("@productsID").Value = sender
        End If

        myReader = mycommand.ExecuteReader

        Me.Bind_Data(myReader)

        myConnection.Close()

       [b] objButton.ForeColor = System.Drawing.Color.Red[/b]

    End Sub

Sorry if it looks like a mess....
 
Works for me if I turn off viewstate for the button

<asp:Button id="Button1" runat="server" Text="Button" EnableViewState="False"></asp:Button>
 
When I add EnableViewState="False" to my button, it somehow messes up the id that I'm sending back.
System.FormatException: Input string was not in a correct format.

I'm not sure how that will solve my problem though. I'm trying to revert the forecolor to its original color once its not the clicked on link...
 
When I set the viewstate from the code behind, it works, but not the way I want. It hides the item, instead of reverting the color...
 
I've done this before, where I save the current selected ID as a session variable (Session("iCur"))...then when a new button is selected, I change back the font in the (Session("iCur")) row of the datalist, and then set the new (Session("iCur")) based on what has been clicked...make any sense?

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Yes, that makes sense, I'll try it out. Thanks!
 
I'm having trouble accessing the item in the datalist...It's a nested datalist. It's parent is a panel, which is nested within another datalist. I can't get to the inner datalist, dlProd!
My aspx code is above. I'll show what I have added in the code behind. I can't initialize dlList.
Code:
Public Sub ProductDetailClicked(ByVal sender As Object, ByVal e As EventArgs)
        Dim objButton As LinkButton
        objButton = CType(sender, LinkButton)
        Dim panel2 As Panel
        Dim dlList As DataListItem
        Dim dlProdList As DataList
        Dim objReader As SqlDataReader

        [b] dlList = CType(dlCategories.findcontrol???, DataListItem)[/b]
        panel2 = CType(objButton.Parent.FindControl("PanelProd"), Panel)
        dlProdList = CType(dlList.FindControl("dlProd"), DataList)
        PanelMainProducts.Visible = False
        PanelProduct.Visible = True
        myConnection = New SqlConnection(conString)
        myConnection.Open()

        mycommand = New SqlCommand("getSpecificProduct", myConnection)
        mycommand.CommandType = CommandType.StoredProcedure

       
        If Session("ProdID") = "" Then

            mycommand.Parameters.Add(New SqlParameter("@productsID", SqlDbType.Int))
            mycommand.Parameters("@productsID").Value = objButton.CommandArgument
            Session("ProdID1") = objButton.CommandArgument
        Else
            mycommand.Parameters.Add(New SqlParameter("@productsID", SqlDbType.Int))
            mycommand.Parameters("@productsID").Value = sender
        End If

        myReader = mycommand.ExecuteReader

        Me.Bind_Data(myReader)

        myConnection.Close()

        objButton.ForeColor = System.Drawing.Color.Red


    End Sub
 
so you'll have to do a find control, within a findcontroL maybe? never done it with that...can the nested datalist have enabled viewstate = false?

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
If I set the viewstate to false for the nested datalist, it actually makes the item disappear once another one is clicked on..it hides it i guess. But I just want to switch the color...
Seems like way too much work for something so simple.
 

<asp:Button id="Button1" runat="server" Text="Button" EnableViewState="False"></asp:Button>

On the button only not the datalist.

If you need to maintain state in your button then use checkai's solution.
 
I'll try to do a findcontrol within a findcontrol...anyone have any examples of this?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top