http://topic.csdn.net/u/20100908/11/903d7344-ab8a-4778-ae4f-3f8199661341.html这次又在一个页面绑定图片,我按照孟子的
 if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            Image image = (Image)e.Item.FindControl("productImg");            System.Data.Common.DbDataRecord d = e.Item.DataItem as System.Data.Common.DbDataRecord;
            if (d["productImg"] == DBNull.Value)
            {
                image.ImageUrl = "~/image/upImage/Default.jpg";
            }
            else
            {
                image.ImageUrl = "~/image/upImage/" + d["productImg"].ToString();
            }
        }错误提示:未将对象引用设置到对象的实例。  System.Data.Common.DbDataRecord d = e.Item.DataItem as System.Data.Common.DbDataRecord;
行 92:             if (d["productImg"] == DBNull.Value)
行 93:             {
行 94:                 image.ImageUrl = "~/image/upImage/Default.jpg";
错误在92行,红色显示。    
纳闷,不知道什么原因

解决方案 »

  1.   

    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
      {
      Image image = (Image)e.Item.FindControl("productImg");
      DataRowView drv =e.Item.DataItem as DataRowView ;
      if(drv !=null)  
      {
       image.ImageUrl = Page.Resolveurl("~/image/upImage/Default.jpg"):Page.Resolveurl("~/image/upImage/" + dr["productImg"].ToString());
       }
      }
      

  2.   

    不可能出现错误的。
    按照你aspx描述的,字段名字productSmallImg还是productImg?
      

  3.   

    这样,把前台代码贴出来:
     <asp:DataList ID="productSortDl"  runat="server" OnItemDataBound="productSortDl_ItemDataBound">
                              <ItemTemplate>
                              <table width="540" border="0" cellpadding="0" cellspacing="0" class="purpleFrame">
                          <tr>
                            <td height="20" background="image/bg6.jpg" bgcolor="#3399ff" class="lv13" style="width: 37px">&nbsp;</td>
                            <td width="451" align="left" background="image/bg6.jpg" bgcolor="#3399ff" class="productsort13"><%# DataBinder.Eval(Container.DataItem,"sortName") %>
                                <asp:Label ID="productSortLb" Text=<%# DataBinder.Eval(Container.DataItem,"sortID") %> runat="server" Visible="false"></asp:Label></td>
                            <td width="50" align="left" background="image/bg6.jpg" bgcolor="#3399ff" class="lv13"><a href="product_more.aspx?sortID=<%# DataBinder.Eval(Container.DataItem,"sortID") %>">
                            <img src="image/more1.gif" width="34" height="9" border="0" /></a></td>
                          </tr>
                          <tr>
                            <td height="130" colspan="3" align="center">
                                <asp:Label ID="Label1" runat="server"></asp:Label>
                                <asp:DataList ID="productDl" runat="server" RepeatDirection="Horizontal">
                          <HeaderTemplate>
                      <table width="500" height="95%" border="0" cellpadding="0" cellspacing="0">
                                <tr>
                          </HeaderTemplate>
                          
                          
                          <ItemTemplate>
                             <td width="125" height="27" align="left" valign="bottom">
                                  <table width="100" border="0" cellspacing="0" cellpadding="0">
                                      <tr>
                                        <td height="90" align="center" class="bianlan">
                                        <a href="product_view.aspx?productID=<%# DataBinder.Eval(Container.DataItem,"productID") %>">
                                        <asp:Image ID="productImg"   ImageUrl='<%# DataBinder.Eval(Container.DataItem,"productSmallImg") %>' runat="server" width="95" height="85" border="0"/>
                                        </a></td>
                                      </tr>
                                      <tr>
                                        <td height="22" align="center">
                                        <a href="product_view.aspx?productID=<%# DataBinder.Eval(Container.DataItem,"productID") %>"><%# DataBinder.Eval(Container.DataItem,"productName") %></a></td>
                                      </tr>
                                  </table>
                              </td>
                          </ItemTemplate>
                          
                          
                         <AlternatingItemTemplate >
                             <td width="125" height="27" align="left" valign="bottom">
                                  <table width="100" border="0" cellspacing="0" cellpadding="0">
                                      <tr>
                                        <td height="90" align="center" class="bianlan">
                                        <a href="product_view.aspx?productID=<%# DataBinder.Eval(Container.DataItem,"productID") %>">
                                        <asp:Image ID="productImg"   ImageUrl='<%# DataBinder.Eval(Container.DataItem,"productSmallImg") %>' runat="server" width="95" height="85" border="0"/>
                                        </a></td>
                                      </tr>
                                      <tr>
                                        <td height="22" align="center">
                                        <a href="product_view.aspx?productID=<%# DataBinder.Eval(Container.DataItem,"productID") %>"><%# DataBinder.Eval(Container.DataItem,"productName") %></a></td>
                                      </tr>
                                  </table>
                              </td>
                         </AlternatingItemTemplate>
                         
                          
                          <FooterTemplate>
                                </tr>
                            </table>
                          </FooterTemplate>
                      </asp:DataList>
    </td>
                          </tr>
                        </table>
                                <table width="100" border="0" cellspacing="0" cellpadding="0">
                                <tr>
                                    <td style="height: 8px">
                                        </td>
                                </tr>
                                </table>
                              </ItemTemplate>
                          </asp:DataList>    
    后台:  protected void productSortDl_ItemDataBound(object sender, DataListItemEventArgs e)
        {    if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Image image = (Image)e.Item.FindControl("productImg");            System.Data.Common.DbDataRecord d = e.Item.DataItem as System.Data.Common.DbDataRecord;
                if (d["productImg"] == DBNull.Value)
                {
                    image.ImageUrl = "~/image/upImage/Default.jpg";
                }
                else
                {
                    image.ImageUrl = "~/image/upImage/" + d["productImg"].ToString();
                }
            }
           
        }
      

  4.   

    你的前台不是绑定了吗?
    <asp:Image ID="productImg" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"productSmallImg") %>' runat="server" width="95" height="85" border="0"/>
    为啥还要在cs再赋值一次呢?当然出错跟这个没关系
      

  5.   

    是不是这样:这个前台是两个Datalist嵌套,而只是外层的datalist绑定: protected void productSortDl_ItemDataBound(object sender, DataListItemEventArgs e)
      {   
      

  6.   

    在内层的ItemDataBound中查询image绑定数据
      

  7.   

    应该在datalist嵌套上出现问题了
      

  8.   

    嵌套就不能那样找 了Image image = (Image)e.Item.FindControl("productImg");
    应该是这个image 为null了
      

  9.   

    DataList嵌套是可以的。但是ItemDataBound事件里面,不能交叉访问对象的
      

  10.   

    你应该在里面那个ItemDataBound事件里FindControl;
      

  11.   

    productDl_ItemDataBound执行以上方法
      

  12.   

    最外层:
    <asp:DataList ID="productSortDl" runat="server" OnItemDataBound="productSortDl_ItemDataBound">里面那个要写(为了明显,改成了AAAA,注意)
    <asp:DataList ID="AAAA" runat="server" RepeatDirection="Horizontal" OnItemDataBound="AAAA_ItemDataBound>
    后台代码
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==ListItemType.AlternatingItem)   
    {   
      DataList aa = (DataList)e.Item.FindControl("AAAA");
      aa.DataSurce = GetProductAdminMsg();
      aa.DataBind();
    }
    }另外一个
    protected void AAAA_ItemDataBound(object sender, DataListItemEventArgs e)
    {
     if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==ListItemType.AlternatingItem)   
    {   
      DataRowView drv = (DataRowView)e.Item.DataItem;  
      Image image = (Image)e.Item.FindControl("productImg");  if (drv["productImg"].ToString() == "")
      {
      image.ImageUrl = @"~/image/upImage/Default.jpg";
      }
      else
      {
      image.ImageUrl = @"~/image/upImage/" + dr["productImg"];
      }
    }}
      

  13.   

    上面的代码都要加
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==ListItemType.AlternatingItem)   
    {   
      DataList aa = (DataList)e.Item.FindControl("AAAA");
      aa.DataSurce = GetProductAdminMsg();
      aa.DataBind();
    }
    }
    也可以写成
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==ListItemType.AlternatingItem)   
    {   
      DataList aa = (DataList)e.Item.FindControl("AAAA");
      aa.ItemDataBound +=new DataListItemEventHandler(AAAA_ItemDataBound);  
      aa.DataSurce = GetProductAdminMsg();
      aa.DataBind();
    }
    }
      

  14.   

    给你一个嵌套绑定的例子
    http://dotnet.aspx.cc/article/f73eeaa9-2bdc-47fd-afd2-59f2fa4897f5/read.aspx
      

  15.   

    System.InvalidCastException: 无法将类型为“System.Data.Common.DbDataRecord”的对象强制转换为类型“System.Data.DataRowView”。源错误: 
    行 116:        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    行 117:        {
    行 118:            DataRowView drv = (DataRowView)e.Item.DataItem;
    行 119:            Image image = (Image)e.Item.FindControl("productImg"); 
    错误在118 ,红色显示
      

  16.   

    DataRowView drv = (DataRowView)e.Item.DataItem;是错误的改成
    System.Data.Common.DbDataRecord d = e.Item.DataItem as System.Data.Common.DbDataRecord;
      

  17.   

      protected void productSortDl_ItemDataBound(object sender, DataListItemEventArgs e)
        {  IProduct product = new Product();  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataList aa = (DataList)e.Item.FindControl("productDl");            aa.DataSource  = product.GetProductAdminMsg();
                aa.DataBind();
            }       
        }    protected void productDl_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                System.Data.Common.DbDataRecord d = e.Item.DataItem as System.Data.Common.DbDataRecord;            Image image = (Image)e.Item.FindControl("productImg");            if (d["productImg"].ToString() == "")
                {
                    image.ImageUrl = @"~/image/upImage/Default.jpg";
                }
                else
                {
                    image.ImageUrl = @"~/image/upImage/" + d["productImg"];
                }
            }        
        }这样写了,但是前台还是只显示一个图片
      

  18.   

    你想要的功能是这样的
    外层的DataList,显示分类,名称,内层的显示图片列表
    productSortDl_ItemDataBound里面应该得到sortId内层的DataList根据这个sortId去读取这个分类下的图片
      

  19.   

    你好,孟子,问题已经解决,在外层datalist加个 Label lbsortID = (Label)e.Item.FindControl("productSortLb");
    然后  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataList aa = (DataList)e.Item.FindControl("productDl");            aa.DataSource = product.GetProductMsgBysortID(Convert.ToInt32(lbsortID.Text));
                aa.DataBind();
            }要学会举一反三那,看来我还是很菜