Tuesday, April 26, 2011

Finding controls in the GridView

Because a GridView renders out multiple rows, each of them contain controls with identical names. Finding the right control requires a trick.

For instance, in the RowDataBound/DataBound event, if we were looking for a particular label so that we could write a value to it 'on the fly', we would use the following code:

Dim lblItemotal As Label = CType(e.Row.FindControl("lblItemTotal"), Label)

The CTYPE function is a conversion function. In this case, it will return all of the data associated with the label we are trying to locate, as a Label object. Once located, we can manipulate this particular instance.

No comments:

Post a Comment