【问题标题】:Image tag within a repeater ASP.NET C#中继器 ASP.NET C# 中的图像标记
【发布时间】:2016-08-05 10:40:15
【问题描述】:

我正在创建一个中继器。如下图所示

<asp:Repeater runat="server" ID="repeaterEvent" OnItemCommand="repeaterEvent_ItemCommand">
                        <ItemTemplate>
                            <div class="jumbotron">
                                <h4><asp:Label ID="Label2" runat="server" Text='<%#Bind("EvtDescription") %>'></asp:Label></h4>   
                                <h4><asp:Label runat="server">Amount Attending: </asp:Label>       
                                <asp:Label ID="Label4" runat="server" Text='<%#Bind("EvtVote") %>'></asp:Label></h4>
                                <asp:Button runat="server" ID="eventButton" Text="Attending" class="btn btn-primary" CommandName="Vote"/>
                            </div>
                        </ItemTemplate>
                    </asp:Repeater>

我还有一张图片,下面有代码:

<img runat="server" id="imageTest" src="imageIDtagName" />

后端代码:

imageTest.Src = "data:Image/png;base64," + str;

这很好用,我可以显示图像。当我尝试在转发器中添加这行代码时,就会出现问题。错误提示“当前上下文中不存在名称'imageTest'。我确定这是一个简单的问题,但提前感谢

【问题讨论】:

  • 你好@Stuart,img 标签在转发器内,所以你需要首先访问转发器项目。
  • 我之前尝试过此示例中的代码,但无法在我的上运行
  • 你在哪里设置.Src?什么方法?

标签: c# asp.net image repeater


【解决方案1】:

由于会为绑定数据中的每个项目创建子控件,因此在引用子控件时,您必须通过项目集合进行引用。 此外,对于数据绑定控件,您将需要使用 FindControl 方法来引用子控件。 您可以通过以下代码使用 ItemDataBound 事件获取图像控件:

 protected void repeaterEvent_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
(e.Item.FindControl("imageTest") as HtmlImage).Src = "data:Image/png;base64," + str;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多