我目前正试图让我的按钮在我的Visual Studio页面中工作。
我正在我的页面中运行一些代码并且它一直会返回此错误:
对象引用未设置为对象的实例。描述:一个 在执行当前Web期间发生了未处理的异常 请求。请查看堆栈跟踪以获取有关的更多信息 错误以及它在代码中的起源。例外细节: System.NullReferenceException:未将对象引用设置为实例 一个对象。
第466行:Delete.OnClientClick = String.Format(" Javascript:HiddenPopup(' deletePopup',{0}');",Cell2)
源文件: \蝙蝠侠\销售\传输\网站\ QuotemanDJ3 \生产\ 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
End Sub
这是我的gridview的标记代码(我的按钮在我的gridview中):
<asp:GridView
ID="GridView1"
runat="server"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource7" DataKeyNames="ServiceID,ID" >
<AlternatingRowStyle BackColor="#CCFFFF" >
</AlternatingRowStyle>
<Columns>
<asp:BoundField DataField="FileName" HeaderText="FileName" SortExpression="FileName" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="Serial No" HeaderText="Serial No" SortExpression="Serial No" />
<asp:BoundField DataField="Record Type" HeaderText="Record Type"
SortExpression="Record Type" Visible="False" >
</asp:BoundField>
<asp:BoundField DataField="ServiceID" HeaderText="ServiceID"
SortExpression="ServiceID" InsertVisible="False" ReadOnly="True" />
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID"
Visible="False" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageURL="~/icons/pdf.gif">
</asp:ImageButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate><asp:Button runat="server" Text="Delete" ID="DeleteInduction" />
<asp:ModalPopupExtender ID="DeleteInduction_ModalPopupExtender" runat="server"
BackgroundCssClass="modalBackground" CancelControlID="cancelButton"
DynamicServicePath="" Enabled="True" PopupControlID="deletePopup"
TargetControlID="DeleteInduction" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
对此有一些指导意见,谢谢你提前。
答案 0 :(得分:1)
在此处考虑您的代码:
Delete = e.Row.FindControl("DeleteInduction")
Delete.OnClientClick = String.Format("Javascript:HiddenPopup('deletePopup', '{0}');", Cell2)
FindControl()
如果doesn't find the control则返回null
。当发生这种情况时,紧接的下一行将抛出该异常,因为您无法从OnClientClick
引用属性(null
)。
只需检查结果是否为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.
}
请记住,例如,控件中的某些事件并不总是适用于数据绑定行。根据包含此代码的事件,您还可以处理不会包含DeleteInduction
控件的页眉和/或页脚行上的代码。在这些情况下,您可能只想完全忽略代码。
例如,在GridView
中,您可以在执行逻辑之前检查行类型:
if (e.Row.RowType == DataControlRowType.DataRow)
{
// put your row logic here
}