Question Accessing a ButtonField in GridView's RowDataBound

njsokalski

Well-known member
Joined
Mar 16, 2011
Messages
102
Programming Experience
5-10
I have a GridView with the following ButtonField:

<asp:ButtonField Text="Delete" ButtonType="Button"/>

I want to edit the generated button during the RowDataBound event, but the statement:

If e.Row.RowType = DataControlRowType.DataRow Then
Me.ClientScript.RegisterStartupScript(Me.GetType(), "testing", String.Format("window.alert('{0}');", CType(e.Row.Cells(0).Controls(0), Button).Attributes.Count), True)
End If

Displays an alert with a value of 0, even though when I do a ViewSource in my browser I see the input tag does have attributes. Also, if I create a window.alert to display the type, it returns System.Web.UI.WebControls.DataControlButton, which Visual Studio's Intellisense doesn't even seem to think exists (and I haven't heard of it or found much on it while searching). It's almost as if a control was created, but none of it's properties or attributes are accessible during RowDataBound (or RowCreated), even though they are there in the browser's View Source. What can I do to access and modify the button generated by ButtonField during the RowDataBound (or RowCreated) event? Thanks.
 
DataControlButton is a Friend class in .Net library, so the type is not available to your code, it inherits Button class that can be used:
Dim btn = CType(e.Row.Cells(0).Controls(0), Button)

You are talking about client side attributes, and they only exist and is accessible to client side script:
btn.OnClientClick = "javascript:alert(this.attributes.length);"

You can also add attributes from server code that will appear client side:
btn.Attributes.Add("some", "thing")
 
Back
Top