Asp.net 对象引用未设置为Gridview删除按钮的对象实例

Asp.net 对象引用未设置为Gridview删除按钮的对象实例,asp.net,vb.net,button,gridview,Asp.net,Vb.net,Button,Gridview,我目前正试图让我的按钮在我的VisualStudio页面中工作 我正在我的页面中运行一些代码,但它不断返回此错误: 对象引用未设置为对象的实例。描述:安 执行当前网站时发生未处理的异常 要求请查看堆栈跟踪以了解有关堆栈的更多信息 错误及其在代码中的起源。例外情况详情: System.NullReferenceException:对象引用未设置为实例 指一个物体 第466行:Delete.OnClientClick= Format(“Javascript:hiddenpoup('deletepou

我目前正试图让我的按钮在我的VisualStudio页面中工作

我正在我的页面中运行一些代码,但它不断返回此错误:

对象引用未设置为对象的实例。描述:安 执行当前网站时发生未处理的异常 要求请查看堆栈跟踪以了解有关堆栈的更多信息 错误及其在代码中的起源。例外情况详情: System.NullReferenceException:对象引用未设置为实例 指一个物体

第466行:Delete.OnClientClick= Format(“Javascript:hiddenpoup('deletepoup',{0}');”,Cell2)

源文件: \蝙蝠侠\Sales\Transfer\Website\QuotemanDJ3\Production\PlantAndMaintenance.aspx.vb 电话:466

我在网上看到了很多解决方案,但我似乎仍然不知道我犯了什么错误,如果我能在这方面得到一些帮助,我将不胜感激

这是我的VB代码:

  Protected Sub Service_History0_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Service_History0.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then

    '-----------------------------------------------------------------

    Dim pdfExtention2, wordExtention2, excelExtention2 As ImageButton

    pdfExtention2 = e.Row.FindControl("PdfExtention2")
    wordExtention2 = e.Row.FindControl("WordExtention2")
    excelExtention2 = e.Row.FindControl("ExcelExtention2")

    If e.Row.DataItem("Extention").ToString = "application/pdf" Then
        pdfExtention2.Visible = True
    ElseIf e.Row.DataItem("Extention").ToString = "application/msword" Then
        wordExtention2.Visible = True
    ElseIf e.Row.DataItem("Extention").ToString = "application/vnd.ms-excel" Then
        excelExtention2.Visible = True
    End If

    '-----------------------------------------------------------------

    Dim ImgBtn As New ImageButton
    ImgBtn = e.Row.FindControl("PdfExtention2")
    ImgBtn.Attributes.Add("onClick", "javascript:window.open('DisplayPM.aspx?ServiceID=" + Service_History0.DataKeys(e.Row.RowIndex).Value.ToString + "');")

    Dim ImgBtn2 As New ImageButton
    ImgBtn2 = e.Row.FindControl("WordExtention2")
    ImgBtn2.Attributes.Add("onClick", "javascript:window.open('DisplayPM.aspx?ServiceID=" + Service_History0.DataKeys(e.Row.RowIndex).Value.ToString + "');")

    Dim ImgBtn3 As New ImageButton
    ImgBtn3 = e.Row.FindControl("ExcelExtention2")
    ImgBtn3.Attributes.Add("onClick", "javascript:window.open('DisplayPM.aspx?ServiceID=" + Service_History0.DataKeys(e.Row.RowIndex).Value.ToString + "');")

    '-----------------------------------------------------------------

    Dim Cell2 As String

    Cell2 = GridView1.DataKeys(e.Row.RowIndex).Values("ServiceID").ToString


    Dim Delete As Button
    Delete = e.Row.FindControl("DeleteInduction")
    Delete.OnClientClick = String.Format("Javascript:HiddenPopup('deletePopup', '{0}');", Cell2)
    connection.Close()


        '-----------------------------------------------------------------



End If
端接头

这是我的gridview的标记代码(我的按钮在我的gridview中):



如果您能提供一些指导,我们将不胜感激,提前谢谢您。

请考虑您的代码:

Delete = e.Row.FindControl("DeleteInduction")
Delete.OnClientClick = String.Format("Javascript:HiddenPopup('deletePopup', '{0}');", Cell2)
FindControl()
返回
null
,如果它是空的。发生这种情况时,紧接着的下一行将抛出该异常,因为您无法从
null
引用属性(
OnClientClick

您只需检查结果是否为
null
,即可防止出现错误:

Delete = e.Row.FindControl("DeleteInduction")
if (Delete != null)
    Delete.OnClientClick = String.Format("Javascript:HiddenPopup('deletePopup', '{0}');", Cell2)
然后问题变成了,当它为
null
时,您想做什么?有什么事吗?上面的代码将自动忽略它。你想做些别的事情吗?这就是你要做的:

Delete = e.Row.FindControl("DeleteInduction")
if (Delete != null)
    Delete.OnClientClick = String.Format("Javascript:HiddenPopup('deletePopup', '{0}');", Cell2)
else
{
    // handle the case where DeleteInduction wasn't found.
}
例如,请记住,控件中的某些事件并不总是应用于数据绑定行。根据包含此代码的事件,您还可以处理不包含
deleteInjection
控件的页眉和/或页脚行上的代码。在这种情况下,您可能希望完全忽略代码

例如,在
GridView
中,您可以在执行逻辑之前检查行类型:

if (e.Row.RowType == DataControlRowType.DataRow)
{
    // put your row logic here
}

对象为空(无值)。调试项目(F5)并将断点(F9)放在文件的第行。祝你好运可能Delete=e.Row.FindControl(“deleteEncurvation”)找不到任何内容且为空。您能否在
Dim Delete…
上方显示其余的VB代码?我猜这不是在正确的地方发生的(它应该在网格行绑定事件中,不记得确切的名称)。我编辑了它,现在它显示了整个代码@Tallmaris和thankyou@lordkainI解决了我的问题,我在VB中引用了错误的Gridview,dope!但我会记住这个答案,因为直到现在我才知道它是如何工作的,谢谢
if (e.Row.RowType == DataControlRowType.DataRow)
{
    // put your row logic here
}