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
lyntron4545Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Darth_helgeCommented:
try this
Protected Sub dgvPartPrice_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles dgvPartPrice.RowDataBound
                Dim btnPrice As Button = CType(e.Row.FindControl("txtMyButtonName"), 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'")
        End Sub

because this event is fired each time a row has been databound you dont need to loop through all the rows in this event.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lyntron4545Author Commented:
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
Darth_helgeCommented:
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
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

lyntron4545Author Commented:
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
Darth_helgeCommented:
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
lyntron4545Author Commented:
Thanks-
I wish I could give you extra points!!!
Darth_helgeCommented:
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 :=)
Darth_helgeCommented:
just referring to the last question in this thread :=)
lyntron4545Author Commented:
Search for "Page level events - Darth_helge" and claim your points.  Thanks again!!!!

LT
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.