我在OnSelectedIndexChanging上遇到错误
我的aspx文件
<asp:GridView CssClass="table table-striped table-hover" ID="product_all_detail_list" runat="server" OnSelectedIndexChanging="product_detail_list_SelectedIndexChanging" OnSelectedIndexChanged="product_all_detail_list_SelectedIndexChanged" >
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<asp:Label ID="lb_id" runat="server" Text='<%#Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
背后的代码
protected void product_detail_list_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
Button btn = sender as Button;
// GridViewRow row = btn.NamingContainer as GridViewRow;
GridViewRow row = (GridViewRow)(((Control)sender).NamingContainer);
all_details.Visible = true;
Label id = (Label)row.FindControl("lb_id");
dal.Fillgrid(product_all_detail_list, q);
}
代码是否正确?
我也尝试过GridViewRow row = btn.NamingContainer as GridViewRow;
但出现类似Object reference not set to an instance of an object.
答案 0 :(得分:0)
当您调用btn.NamingContainer时,您的btn不在GridView中,它将提供ContentPlaceHolder对象。
将ContentPlaceHolder投射到GridViewRow时会出现异常
尝试使用如下所示的SelectedIndex获取所选行。
GridViewRow row = product_all_detail_list.Rows[product_all_detail_list.SelectedIndex];
有关更多信息,请查看此url