你调用的对象是空的。 :转发器内的网格视图

时间:2013-07-04 16:44:21

标签: asp.net .net

gridview放在转发器中, 它在itemdata绑定上显示错误

代码,

  <asp:Repeater ID="rptSearchResult" runat="server" 
                OnItemDataBound="rptSearchResult_ItemDataBound">
          <HeaderTemplate>
         <table border="0">
             <tr>
                 <td> Cost Page</td>
                 <td> Cost Page Description</td>
                 <td> Vendor Name</td>
                 <td> Bill Type</td>
             </tr>
      </HeaderTemplate>

      <ItemTemplate>
         <tr>
            <td> <%# Eval("CostPage")%></td>
               <td><%# Eval("CostPageDescription")%> </td>
               <td> <%# Eval("VendorName")%> </td>
               <td><%# Eval("BillType")%> </td>
         </tr>
           <tr>
            <td>
               <asp:GridView ID="gvDetails" runat="server" >
                   <Columns>  
                     <asp:TemplateField HeaderText="">
                     <ItemTemplate> 
                         <asp:CheckBox ID="chkSelect" runat="server" />
                     </ItemTemplate>
                    </asp:TemplateField>              
                     <asp:BoundField DataField="ItemId" HeaderText="Item ID"/>                    
                     <asp:BoundField DataField="ItemDescription"  HeaderText="Item Description"/>
                     <asp:BoundField DataField="BrandCode"   HeaderText="Brand Code"/>                    

                    </Columns> 

               </asp:GridView>  </td>
         </tr>
      </ItemTemplate>

      <FooterTemplate>
         </table>
      </FooterTemplate>

        </asp:Repeater>
<。>在.cs文件中,

    protected void rptSearchResult_ItemDataBound(
       object sender, RepeaterItemEventArgs e)
    {
        GridView gd = (GridView)e.Item.FindControl("gvDetails");


        details e1 = new details();
        e1.itemid= 1;
        e1.itemdesc = "item1";
        e1.brandcode = "BBB";

        List<details> employees = new List<details>();
        employees.Add(e1);

        gd.DataSource = employees;
        gd.DataBind();

}

2 个答案:

答案 0 :(得分:2)

检查您的gd天气天气是否为空,而不是执行您的特定条件。因此代码应如下所示:

GridView gd = (GridView)e.Item.FindControl("gvDetails");

if(gd != null) {
  gd.DataSource = employees;
  gd.DataBind();
}

修改 错误即将发生,因为DataField="ItemDescription"的{​​{1}}中的名称与数据绑定时指定的数据源员工属性不匹配。标记代码应为

asp:BoundField

答案 1 :(得分:0)

通过添加以下检查解决了该问题:

 if( (e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) 
  {  
    //Non-null value for grid
  }

现在,gridview是非空值。