如何在listview eval上的mssql中检索二进制数据

时间:2016-09-29 08:42:41

标签: c# asp.net sql-server

我想在c#的列表视图中绑定一个图像(以二进制格式列名图像(varbinary)存储)。 我正在尝试这两个但没有工作..

 <img src='<%# string.Format("data:image/gif;base64,{0}",Convert.ToBase64String((byte[])Eval("image")))%>'/>--%>
                <%--<asp:Image ID="Image10" runat="server" Height="100px" ImageUrl='<%# "ShowImage.ashx?id=" + Eval("image") %>' GenerateEmptyAlternateText="True" Visible='<%# ShowImg(Eval("image")) %>' />--%>

这是代码:

<div>
        <h3 class="calDetail">Event Details</h3>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:VeteransDirectoryConnectionString %>" SelectCommand=
        "SELECT [EventID], [OrganizationName], [Title], [DescriptionShort], [DescriptionLong], [Location], [EventDate], [URL], (CASE WHEN URL IS NULL OR URL = '' THEN CAST(0 AS bit) ELSE CAST(1 AS bit) END) AS URLLink, [FileName], (CASE WHEN FileName IS NULL OR FileName = '' THEN CAST(0 AS bit) ELSE CAST(1 AS bit) END) AS FileLink, [EventTime],image FROM [t_Event] WHERE ([EventID] = @EventID)">


             <SelectParameters>
                <asp:QueryStringParameter Name="EventID" QueryStringField="eventid" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>
        <br />
        <div style="padding: 0px 10px 20px 10px;">
        <asp:ListView ID="ListView1" runat="server" DataKeyNames="EventID" DataSourceID="SqlDataSource1">
            <EmptyDataTemplate>
                <span>No data for this event was found.</span>
            </EmptyDataTemplate>
            <ItemTemplate>
                <span class="listCalLabel">Organization:</span> &nbsp;
                <asp:Label ID="OrganizationNameLabel" runat="server" Text='<%# Eval("OrganizationName") %>' CssClass="listCalText" />
                <br /><br />
                <span class="listCalLabel">Event:</span> &nbsp;
                <asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' CssClass="listCalText" />
                <br /><br />
                <span class="listCalLabel">Short Description:</span> &nbsp;
                <asp:Label ID="DescriptionShortLabel" runat="server" Text='<%# Eval("DescriptionShort") %>' CssClass="listCalText" />
                <br /><br />
                <span class="listCalLabel">Long Description:</span> &nbsp;
                <asp:Label ID="DescriptionLongLabel" runat="server" Text='<%# Eval("DescriptionLong") %>' CssClass="listCalText" />
                <br /><br />
                <span class="listCalLabel">Location:</span> &nbsp;
                <asp:Label ID="LocationLabel" runat="server" Text='<%# Eval("Location") %>' CssClass="listCalText" />
                <br /><br />
                <span class="listCalLabel">Event Date:</span> &nbsp;
                <asp:Label ID="EventDateLabel" runat="server" Text='<%# Eval("EventDate", "{0:MM/dd/yyyy}") %>' CssClass="listCalText" />
                <br /><br />
                <span class="listCalLabel">Event Time:</span> &nbsp;
                <asp:Label ID="EventTimeLabel" runat="server" Text='<%# Eval("EventTime") %>' CssClass="listCalText" />
                <br /><br />
                <span class="listCalLabel"><asp:Label ID="lblURL" runat="server" Visible='<%# Eval("URLLink") %>' Text="Additional Info (website):" ></asp:Label></span>
                <br />
                <asp:HyperLink ID="URLHyperLink" runat="server" Text='<%# Eval("URL") %>' NavigateUrl='<%# Eval("URL") %>' Visible='<%# Eval("URLLink") %>' Target="_blank" CssClass="listLink" />
                <br /><br />
                <span class="listCalLabel"><asp:Label ID="lblFile" runat="server" Visible='<%# Eval("FileLink") %>' Text="Additional Info (file):"></asp:Label></span>
                <br />
                <asp:HyperLink ID="URLFile" runat="server" Text='<%# Eval("FileName") %>' NavigateUrl='<%# "EventFiles/" + Eval("FileName") %>' Visible='<%# Eval("FileLink") %>' Target="_blank" CssClass="listLink" />
                <br /><br />
<%--                <img src='<%# string.Format("data:image/gif;base64,{0}",Convert.ToBase64String((byte[])Eval("image")))%>'/>--%>
                <%--<asp:Image ID="Image10" runat="server" Height="100px" ImageUrl='<%# "ShowImage.ashx?id=" + Eval("image") %>' GenerateEmptyAlternateText="True" Visible='<%# ShowImg(Eval("image")) %>' />--%>
                <br /></span>
            </ItemTemplate>
            <LayoutTemplate>
                <div id="itemPlaceholderContainer" runat="server" style="">
                    <span runat="server" id="itemPlaceholder" />
                </div>
                <div style="">
                </div>
            </LayoutTemplate>
        </asp:ListView>
        <asp:Button ID="btnOK" runat="server" OnClientClick="window.close(); return false;" Text="Close Event Detail" CssClass="btn btn-default" />
        <br />
        <br />

        </div>
    </div>

我该怎么办? 如何检索图像并在webform中显示??

1 个答案:

答案 0 :(得分:0)

您可以使用处理程序在gridview中显示图像,您的html标记看起来像

Gridview ItemTemplate将图像控制src设置为src =〜/ getImage.ashx?id =“+ id

其中getImage.ashx是返回MemoryStream((byte [])img)的处理程序;

有关详细信息,请参阅http://www.aspdotnet-suresh.com/2015/07/save-retrieve-display-binary-images-in-gridview-from-database-in-aspnet-csharp-vbnet.html