本文介绍了空参考异议。你调用的对象是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 当我将数据表链接到页脚插入的asp.net网格视图中的下拉列表时,我收到了上述错误。 /> 我已经立即检查了窗口,数据表有2条记录(datatable.rows.count)。 以下是asp和vb中的代码...请建议 Asp。 < asp:TemplateField HeaderText = 输入 HeaderStyle-HorizontalAlign = 中心 > < EditItemTemplate > < asp:DropDownList ID = ddlGridType runat = server DataTextField = BOMIT DataValueField = BOMIT > < / asp:DropDownList > < / EditItemTemplate > < ItemTemplate > < asp:标签 ID = lblGridType runat = server 文字 =' <% #Eval( BOMIT)%> ' > < / asp:Label > < / ItemTemplate > < FooterTemplate > < asp:DropDownList ID = ddlNewGridType runat = server DataTextField = BOMIT DataValueField = BOMIT > < / asp:DropDownList > < / FooterTemplate > < / asp:TemplateField > VB: 受保护的 Sub DataGridView1_RowDataBound(发件人 As 对象,e As GridViewRowEventArgs)句柄 DataGridView1.RowDataBound 如果 e.Row.RowType = DataControlRowType.Footer 然后 Dim daGridTypes 作为 SqlDataAdapter = 新 SqlDataAdapter daGridTypes.SelectCommand = 新 SqlCommand ( 从bom中选择不同的BOMIT,连接) Dim tbGridTypes As DataTable = 新 DataTable connection.Open() daGridTypes.Fill(tbGridTypes) connection.Close() ' 要排序DATATABLE Dim dataGridView As 新 DataView(tbGridTypes) dataGridView.Sort = BOMIT ASC tbGridTypes = 新 DataTable tbGridTypes = dataGridView.ToTable() ddlNewGridType = e.Row.FindControl( ddlNewGridype) ********** ddlNewGridType .DataSource = tbGridTypes ddlNewGridType.DataTextField = BOMIT ddlNewGridType.DataValueField = BOMIT ddlNewGridType.DataBind() 结束 如果 结束 Sub 解决方案 你有一个错字,在控件ID中缺少一个字母。 ddlNewGridType = e.Row.FindControl( ddlNewGridype) 应该是 ddlNewGridType = e.Row.FindControl( ddlNewGridType) 它是总是一个好主意,检查返回的值是否为null,以便捕获并记录这些错误。这可以防止.aspx文件的更改无声地破坏.aspx.cs文件。 ddlNewGridType = e.Row.FindControl( ddlNewGridype) if (ddlNewGridType == null ) logger.Error( 哎呀,我找不到'ddlNewGridType'。); 你不能直接访问控制通过id在项目,页眉或页脚等模板中 你可以做的是找到控件并设置数据源。添加到下面的行 如果 e.Row.RowType = DataControlRowType.Footer 那么 Dim ddlNewGridType As DropDownList = DirectCast (e.Row .FindControl( ddlNewGridType),DropDownList) Hi All,I am getting the above error while i have linked a datatable to a dropdownlist in asp.net Grid view in Footer for Insert.I have checked in immediate window and datatable is having 2 records (datatable.rows.count).following is the code in asp and vb... Please sugggestAsp.<asp:TemplateField HeaderText="Type" HeaderStyle-HorizontalAlign="Center"> <EditItemTemplate> <asp:DropDownList ID="ddlGridType" runat="server" DataTextField="BOMIT" DataValueField="BOMIT"> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblGridType" runat="server" Text='<%# Eval("BOMIT")%>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:DropDownList ID="ddlNewGridType" runat="server" DataTextField="BOMIT" DataValueField="BOMIT"> </asp:DropDownList> </FooterTemplate> </asp:TemplateField>VB:Protected Sub DataGridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles DataGridView1.RowDataBound If e.Row.RowType = DataControlRowType.Footer Then Dim daGridTypes As SqlDataAdapter = New SqlDataAdapter daGridTypes.SelectCommand = New SqlCommand("select distinct BOMIT from bom", connection) Dim tbGridTypes As DataTable = New DataTable connection.Open() daGridTypes.Fill(tbGridTypes) connection.Close() 'To SORT THE DATATABLE Dim dataGridView As New DataView(tbGridTypes) dataGridView.Sort = "BOMIT ASC" tbGridTypes = New DataTable tbGridTypes = dataGridView.ToTable() ddlNewGridType = e.Row.FindControl("ddlNewGridype") **********ddlNewGridType.DataSource = tbGridTypes ddlNewGridType.DataTextField = "BOMIT" ddlNewGridType.DataValueField = "BOMIT" ddlNewGridType.DataBind() End If End Sub 解决方案 You have a typo, missing a letter in the control ID.ddlNewGridType = e.Row.FindControl("ddlNewGridype")should beddlNewGridType = e.Row.FindControl("ddlNewGridType")It is always a good idea to check that the returned value is not null, so that you catch and log these errors. This prevents changes to the .aspx file from silently breaking the .aspx.cs file.ddlNewGridType = e.Row.FindControl("ddlNewGridype")if (ddlNewGridType == null) logger.Error("Oops, I can't find 'ddlNewGridType'.);you can't directly access control by id inside the templates like item, header or footerwhat you can do is Find the control and set the data source. add below lineIf e.Row.RowType = DataControlRowType.Footer Then Dim ddlNewGridType As DropDownList = DirectCast(e.Row.FindControl("ddlNewGridType"), DropDownList) 这篇关于空参考异议。你调用的对象是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-22 19:49