Link to home
Create AccountLog in
Avatar of lyntron4545
lyntron4545

asked on

Cast LiteralControl to WebControls.button

Is there any way to add attributes to a template button in a gridview.  I've tried to Ctype the control in the column but it gives me an invalid cast expection.  What I'm trying to do is add rollover effects thru a codebehind page.  Any ideas?

Code:
Protected Sub dgvPartPrice_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles dgvPartPrice.RowDataBound
            Static intRowCounter As Integer = 0

            Dim i As Integer
            For i = 0 To dgvPartPrice.Rows.Count - 1
                Dim btnPrice As Button = CType(dgvPartPrice.Rows(i).Cells(0).Controls.Item(0), Button)
               
                btnPrice.Attributes.Add("OnMouseOver", "this.style.cursor='hand';this.style.border='Outset';style.borderWidth='2'")
                btnPrice.Attributes.Add("OnMouseOut", "this.style.cursor='default';this.style.border='Solid'; style.borderWidth='1'")
            Next
        End Sub
ASKER CERTIFIED SOLUTION
Avatar of Darth_helge
Darth_helge

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of lyntron4545
lyntron4545

ASKER

Thanks for the reply
I haven't tested your code yet but I should note that this gridview is within a content page.  Would that cause any problems?  Also, as I was debugging it appeared that the id was random or perhaps unique to the app itself.  Is there any way around that?  Thanks for the help and I'll let you know tomorrow about your suggestion.

Thanks-
LT
what the btnPrice button is called at client side is uninteresting. You just call it by the server id.

If your button in the gridview is called "btnPrice" then you just do
Dim btnPrice As Button = CType(e.Row.FindControl("btnPrice"), Button)

gridview sorts out which button to fetch because he knows which row he is on
Darth_helge-
You were completely right!!!!!  That was a simple solution I can't believe that it eluded me for so long.  Thanks for your second post that makes alot of since.  Say do you know of a website that shows a list of all asp events and when they fire.  I can find some events.  Thanks again for your help this really saved my bacon!!!!!

Thank you-
Lyntron4545
well i can give you one right now
http://weblogs.asp.net/jeff/archive/2004/07/04/172683.aspx

but if you want to put in master page events too i have some info here
http://xucanzhao.cnblogs.com/archive/2006/02/26/338197.html
Thanks-
I wish I could give you extra points!!!
actually you could..
the event question is a standalone question, so you can ask a new question called 'points for darth helge' so i can insert a comment, then you accept it :=)
just referring to the last question in this thread :=)
Search for "Page level events - Darth_helge" and claim your points.  Thanks again!!!!

LT