【问题标题】:Image url in code-behind not working using asp.net代码隐藏中的图像 url 无法使用 asp.net
【发布时间】:2017-07-20 12:37:36
【问题描述】:

我在使用 asp.net 在我的网站中显示图像时遇到问题。最初,当我还没有在我的转发器中集成强类型数据控件时,我已经成功地完成了所有工作。这是之前的代码:

public void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Image img = e.Item.FindControl("brandImage") as Image;
    img.ImageUrl = "../Images/Guitar Brands/Guitar Items/" + 
      ((DataRowView)e.Item.DataItem).Row["itemimage1"];
}

当我仍在使用数据集时,上面的代码可以完美运行。但是当我开始使用强类型中继器时,它给了我这个服务器错误:

无法转换类型为“System.Data.Entity.DynamicProxies”的对象。 stringInstrumentItem_F14A58FE0974F90187F45D6 203769613D21C52F3672B47D240863DC806C87C17' 键入 'System.Data.DataRowView'。

描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。

异常详细信息:System.InvalidCastException:无法转换对象 'System.Data.Entity.DynamicProxies.
类型的 stringInstrumentItem_F14A58FE0974F90187F45D6203769613D21C52F3672B47D24086 3DC806C87C17' 键入 'System.Data.DataRowView'。

来源错误:

第 26 行:{ 第 27 行:图像 img = e.Item.FindControl("brandImage") as Image;第 28 行:
img.ImageUrl = "../Images/Guitar Brands/Guitar Items/" +
((DataRowView)e.Item.DataItem).Row["itemimage1"];第 29 行:}
第 30 行:

源文件:c:\Users\User1\Documents\Visual Studio
2015\WebSites\MusicStore\Pages\GuitarItemsFront.aspx.cs 行:28

堆栈跟踪:

[InvalidCastException:无法转换类型的对象
'System.Data.Entity.DynamicProxies。 stringInstrumentItem_F14A58FE0974F90187F45D6203769613D21C52F 3672B47D240863DC806C87C17' 键入'System.Data.DataRowView'。] Pages_GuitarItemsFront.repeater_ItemDataBound(对象发送者,
RepeaterItemEventArgs e) 在 c:\Users\User1\Documents\Visual Studio
2015\WebSites\MusicStore\Pages\GuitarItemsFront.aspx.cs:28 System.Web.UI.WebControls.Repeater.OnItemDataBound(RepeaterItemEventArgs e) +111 System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +138 System.Web.UI.WebControls.Repeater.AddDataItemsIntoItemsArray(IEnumerable 数据源,布尔使用数据源)+217 System.Web.UI.WebControls.Repeater.PostGetDataAction(IEnumerable
数据源)+71 System.Web.UI.WebControls.Repeater.CreateControlHierarchy(布尔
使用数据源)+220 System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +62 System.Web.UI.WebControls.Repeater.DataBind() +77 System.Web.UI.WebControls.Repeater.EnsureDataBound() +58 System.Web.UI.WebControls.Repeater.OnPreRender(EventArgs e) +15 System.Web.UI.Control.PreRenderRecursiveInternal() +88 System.Web.UI.Control.PreRenderRecursiveInternal() +160 System.Web.UI.Control.PreRenderRecursiveInternal() +160 System.Web.UI.Control.PreRenderRecursiveInternal() +160 System.Web.UI.Control.PreRenderRecursiveInternal() +160 System.Web.UI.Page.ProcessRequestMain(布尔
includeStagesBeforeAsyncPoint,布尔型 includeStagesAfterAsyncPoint) +883

错误指向图像路径。我想知道如何使用转发器中的强类型数据控件使我的图像 url 再次工作。我还将包括 aspx 文件的代码。

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="repeater_ItemDataBound" ItemType="stringInstrumentItem" SelectMethod="GetItemData">
    <ItemTemplate>
        <div class="one-two">
            <asp:LinkButton ID="linkButton" OnClick="Repeater1_OnClick" runat="server" CommandArgument='<%# Item.brandId + ";" + Item.model %>'>
                 <asp:Image ID="brandImage" runat="server" ImageUrl='<%# Item.itemimage1 %>' height="130px" width="350px" />
             </asp:LinkButton>
            <div class="content">
                <div id="label"><%# Item.brand.name %> <%# Item.model %></div>
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>

为了了解我的数据库是什么,这里是表格:

表stringInstrumentItem(列brandId为外键,引用表brand的主键,也叫brandId):


itemId  brandId  型号
1         1            xyz
2         1            abc
3         2            hjk

表品牌(其主键名为brandId,由表strinInstrumentItem引用):


brandId  名称  图片
1         Ibanez    xyz.jpg
2         挡泥板    abc.jpg
3         吉布森    hjk.jpg

这里也是GetItemData方法的代码:

public List<stringInstrumentItem> GetItemData()
{
    MusicStoreDBEntities obj = new MusicStoreDBEntities();
    List<stringInstrumentItem> name = new List<stringInstrumentItem>();
    name = (from g in obj.stringInstrumentItems where g.brand.name == guitarName && g.type == "Guitar" select g).ToList();
    return name;
}

【问题讨论】:

  • e.Item.DataItem的类型是什么?
  • 这与图片无关。 e.Item.DataItem 不是 DataRowView,它是一个名为 stringInstrumentItem 的实体类型(由 Entity Framework 子类化以用于延迟加载),如错误所示。改为转换为该类型。
  • 如何将数据分配给DataSource?您能否也向我们展示一下查询?
  • @Win - 我已在问题末尾包含代码。
  • ((DataRowView)e.Item.DataItem) 中的DataRowView 替换为stringInstrumentItem...

标签: c# asp.net sql-server database


【解决方案1】:

您需要在查询中使用 Include 关键字。 例如,

List<stringInstrumentItem> name = obj.stringInstrumentItems
        .Include(x => x.brand)
        .Where(g => g.brand.name == guitarName && g.type == "Guitar")
        .Select(g => g)
        .ToList();

然后从标记中删除 ImageUrl,并从后面的代码中分配它。

<asp:Image ID="brandImage" runat="server" height="130px" width="350px" />

代码背后

protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var stringInstrumentItem = e.Item.DataItem as stringInstrumentItem;

        Image img = e.Item.FindControl("brandImage") as Image;
        img.ImageUrl = string.Format("~/Images/Guitar Brands/Guitar Items/{0}", stringInstrumentItem.brand.image);
    }
}

注意:如果可能,文件夹名称中不要有空格。

【讨论】:

  • 嗯..实际上查询正在运行。我已经通过将图像 url 中的转换从 DataRowView 更改为 stringInstrumentItem 来解决它。但我对你的方法很感兴趣,这也行。
  • 通常,我们使用 Include 关键字显式加载关系表。 EF 有时会自动为我们拉取这些表,但情况并非总是如此。
  • 我明白了。伟大的洞察力。
猜你喜欢
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 2010-09-26
  • 1970-01-01
  • 1970-01-01
  • 2012-07-10
  • 1970-01-01
  • 2011-06-08
相关资源
最近更新 更多