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?
 
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.
0
 
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
0
 
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
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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

LT
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.