Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.20/5 (3 votes)
See more:
when view in browser, i got this error:
Unable to cast object of type 'ASP.webform1_aspx' to type 'System.Web.UI.WebControls.Button'.

how should i solve this problem?

HTML
Line 7:          If Not Page.IsPostBack Then
Line 8:              Dim rowIndex As Integer = 0
Line 9:              Dim btn As Button = DirectCast(sender, Button)
Line 10:             'Get reference of the gridview row of the button clicked.
Line 11:             Dim GridView2 As GridViewRow = DirectCast(btn.Parent.Parent, GridViewRow)
Line 12:             Dim txt1 As TextBox = TryCast(GridView2.Cells(1).FindControl("TextBox1"), TextBox)
Posted
Updated 28-Jan-12 7:20am
v2

1 solution

The answer is in the error message, somewhere you are trying to cast the webform itself (rather than a button element on the form) to type Button
The offending line is likely to be :

Dim btn As Button = DirectCast(sender, Button)


But it is difficult to tell from your code (we don't know what is calling this function and you don't say where the error occurs). If it is this line, sender is the form itself rather than a button. You can check by putting a breakpoint on the line and hovering over sender when it is hit to inspect what it is.

It is impossible to give you an exact fix with the supplied code, but once you have worked out where the error is happening, you should get the button you want to cast from the form object that is currently throwing the error.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900