如何从后台代码引用动态生成ID的Div?空引用异常求助
动态生成ID的Div空引用异常排查与解决
问题场景
我在尝试引用一个包含动态ID的Div时,始终触发空引用异常,divElement始终返回Nothing。该Div嵌套在gvRenewals GridView控件中,ID通过<%# Eval("Agreement") %>动态生成,相关代码如下:
<div id="div<%# Eval("Agreement") %>" class="childGrid" style="display:none;position:relative;left:25px;"> <asp:GridView ID="gvHistoricalRenewals" OnRowCommand="gvHistoricalRenewals_RowCommand" OnRowDataBound="gvHistoricalRenewals_RowDataBound" runat="server" AutoGenerateColumns="false" DataKeyNames="BatchNum" EmptyDataText="No agreements found" Width="80%" CellPadding="4" ForeColor="#333333" GridLines="Both"> <HeaderStyle BackColor="Maroon" Font-Bold="true" ForeColor="White" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <Columns> <asp:ButtonField DataTextField="Agreement" CommandName="Agreement" HeaderText="Agreement" SortExpression="Agreement" meta:resourceKey="lblAgreement" ControlStyle-ForeColor="#822402" /> <asp:BoundField DataField="CoverageDate" DataFormatString="{0:dd/MM/yyyy}" HeaderText="Coverage Date" SortExpression="CoverageDate" meta:resourceKey="lblCoverageDate" ReadOnly="true" /> <asp:BoundField DataField="ExpirationDate" DataFormatString="{0:dd/MM/yyyy}" HeaderText="Expiration Date" SortExpression="ExpirationDate" meta:resourceKey="lblExpirationDate" ReadOnly="true" /> <asp:BoundField DataField="AccountName" HeaderText="Name" SortExpression="AccountName" ReadOnly="true" /> <asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" meta:resourceKey="lblLocation" ReadOnly="true" /> <asp:BoundField DataField="BillTo" HeaderText="Bill To" SortExpression="BillTo" meta:resourceKey="lblBillTo" ReadOnly="true" /> <asp:ButtonField CommandName="ShowEquipment" HeaderText="Equipment" Text="Show" ControlStyle-ForeColor="#822402" /> <asp:BoundField DataField="BatchNum" SortExpression="BatchNum" Visible="false" /> </Columns> </asp:GridView> </div>
我尝试了两种引用方式,均未成功:
第一种写法:
Dim divElement As HtmlGenericControl = CType(gvRenewals.FindControl("div" & agreement), HtmlGenericControl)
第二种写法:
Dim divElement As HtmlGenericControl = CType(gvRenewals.FindControl("div" & "<%=" & agreement.ToString & "%>"), HtmlGenericControl)
问题原因与解决方法
1. 核心问题:控件层级查找错误
gvRenewals作为GridView控件,其直接子控件是每一行的GridViewRow,而非Div。直接调用gvRenewals.FindControl只会查找GridView根节点的直接子控件,无法定位到某一行内部的Div。
2. 正确的查找逻辑
- 先定位Div所在的目标
GridViewRow(即gvRenewals中的某一行) - 在目标行内调用
FindControl查找动态ID的Div
3. 具体实现示例
假设你是在gvRenewals的事件(如RowCommand、RowDataBound)中操作,或通过按钮获取对应行:
' 示例:通过CommandArgument获取目标行索引 Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument) Dim targetRow As GridViewRow = gvRenewals.Rows(rowIndex) ' 在目标行内查找Div Dim divElement As HtmlGenericControl = CType(targetRow.FindControl("div" & agreement), HtmlGenericControl) ' 空引用检查 If divElement IsNot Nothing Then ' 执行操作,例如切换显示状态 divElement.Style("display") = "block" End If
4. 额外注意事项
- 确保
agreement变量的值与Eval("Agreement")生成的内容完全一致(注意大小写、空格、特殊字符) - 若Div在
TemplateField模板列中,必须在对应的GridViewRow内查找,而非GridView根节点 - 后端代码无需添加前端渲染语法
<%= %>,直接拼接动态ID字符串即可
内容的提问来源于stack exchange,提问作者WhiteGlint
相关产品推荐
相关产品推荐

