错误:无法将类型为"ASP.webform1_aspx"的对象转换为类型为"System.Web.UI.WebControls.Button"的对象. [英] error: Unable to cast object of type 'ASP.webform1_aspx' to type 'System.Web.UI.WebControls.Button'.

查看:181
本文介绍了错误:无法将类型为"ASP.webform1_aspx"的对象转换为类型为"System.Web.UI.WebControls.Button"的对象.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在浏览器中查看时,出现此错误:
无法将类型为``ASP.webform1_aspx''的对象转换为类型为``System.Web.UI.WebControls.Button''的对象.

我该如何解决这个问题?

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?

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)

推荐答案

答案是在错误消息中,您尝试将Webform本身(而不是表单上的按钮元素)强制转换为键入Button
令人反感的行可能是:

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)



但是很难从您的代码中分辨出来(我们不知道该函数在调用什么,您也不说错误发生在何处).如果是此行,则sender是表单本身而不是按钮.您可以通过以下方式进行检查:在线路上放置一个断点,并在碰到发送方时将其悬停在其上方以检查其内容.

用所提供的代码无法为您提供确切的解决方法,但是一旦确定了发生错误的位置,就应该从当前引发错误的表单对象中获取要转换的按钮.



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.


这篇关于错误:无法将类型为"ASP.webform1_aspx"的对象转换为类型为"System.Web.UI.WebControls.Button"的对象.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆