简体   繁体   English

无法将类型为“ System.Web.UI.WebControls.ContentPlaceHolder”的对象转换为类型为“ System.Web.UI.WebControls.GridViewRow”的对象

[英]Unable to cast object of type 'System.Web.UI.WebControls.ContentPlaceHolder' to type 'System.Web.UI.WebControls.GridViewRow'

I am getting the error on OnSelectedIndexChanging 我在OnSelectedIndexChanging上遇到错误

My aspx file 我的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>

The code behind 背后的代码

 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);
}

Is the code is correct? 代码是否正确? I also tried for GridViewRow row = btn.NamingContainer as GridViewRow; 我也尝试了GridViewRow row = btn.NamingContainer as GridViewRow;

but getting an error like Object reference not set to an instance of an object. 但收到诸如Object reference not set to an instance of an object.类的错误, Object reference not set to an instance of an object.

your btn is not inside the GridView when you call btn.NamingContainer it will give ContentPlaceHolder object. 当您调用btn.NamingContainer时,您的btn不在GridView中,它将提供ContentPlaceHolder对象。

you will get an exception when you cast ContentPlaceHolder to GridViewRow 当您将ContentPlaceHolder转换为GridViewRow时,将获得异常

try with SelectedIndex like below to get the selected row. 尝试如下所示的SelectedIndex获取选定的行。

GridViewRow row = product_all_detail_list.Rows[product_all_detail_list.SelectedIndex];

for More information take a look at this url 有关更多信息,请查看此URL

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型为“ Obout.Grid.GridTemplate”的对象转换为类型为“ System.Web.UI.WebControls.GridViewRow”的对象。 不解决 - Unable to cast object of type 'Obout.Grid.GridTemplate' to type 'System.Web.UI.WebControls.GridViewRow'. not resolving 无法将类型为“ System.Web.UI.HtmlControls.HtmlForm”的对象转换为类型为“ System.Web.UI.WebControls.GridViewRow”的对象 - Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlForm' to type 'System.Web.UI.WebControls.GridViewRow' 从对象类型System.Web.UI.WebControls.GridViewRow到已知托管提供程序本机类型的映射不存在 - No mapping exists from object type System.Web.UI.WebControls.GridViewRow to a known managed provider native type 无法将类型为“ System.Web.UI.WebControls.Button”的对象转换为类型为“ System.Web.UI.WebControls.CheckBox”的对象 - Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.CheckBox' 无法将类型为“ System.Web.UI.WebControls.Button”的对象转换为类型为“ System.Web.UI.WebControls.LinkBut​​ton”的对象 - Unable to cast object of type 'System.Web.UI.WebControls.Button' to type 'System.Web.UI.WebControls.LinkButton' 无法将类型为“ System.Web.UI.WebControls.GridView”的对象转换为类型为“ System.Web.UI.WebControls.LinkBut​​ton”的对象。 在使用分页时 - Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.LinkButton'. while using pagination 无法将类型为“ System.Web.UI.WebControls.ImageButton”的对象转换为类型为“ System.Web.UI.WebControls.LinkBut​​ton”的对象 - Unable to cast object of type 'System.Web.UI.WebControls.ImageButton' to type 'System.Web.UI.WebControls.LinkButton' 无法将类型为“ System.Web.UI.WebControls.LinkBut​​ton”的对象转换为类型为“ System.Web.UI.WebControls.Button”) - Unable to cast object of type 'System.Web.UI.WebControls.LinkButton' to type 'System.Web.UI.WebControls.Button') 无法将类型为“ System.Web.UI.LiteralControl”的对象转换为类型为“ System.Web.UI.WebControls.Button”的对象 - Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.Button' 无法将类型为“ System.Web.UI.WebControls.Label”的对象转换为类型为“ System.Web.UI.WebControl”的对象 - Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.Web.UI.WebControl
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM