Asp.net c#显示来自单个数据库记录的多个图像

时间:2016-05-25 14:29:42

标签: c# asp.net

构建我的第一个.net应用程序,所以请耐心等待我的新手。

我正在从用户收集数据并存储到SQL中。每条记录有几个数据字段和最多四个图像(是的会导致非常大的记录,但是体积很小)。

现在我需要能够将这些图像显示给用户。我想使用类似于下面的页面布局来做到这一点,但不知道如何实现可以处理这个问题的处理程序。页面显示的记录来自查询字符串,唯一值为questionID。

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Submit_Detail.aspx.cs" Inherits="cs1.Submit.Submit_Detail" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<asp:FormView ID="Submit_tDetail" runat="server" ItemType="cs1.Models.Questions" SelectMethod ="GetQuestion" RenderOuterTable="false">
    <ItemTemplate>
        <div>
            <h1><%#:Item.KeyObjective %></h1>
        </div>
        <br />
        <table>
            <tr>
                <td>
                    <img src="<%#:Item.ImageFile %>" style="border:solid; height:300px" alt="No Image"/>
                </td>
                <td>
                    <img src="<%#:Item.ImageFile2 %>" style="border:solid; height:300px" alt="No Image" />
                </td>
            </tr>
            <tr>
                <td>
                    <img src="<%#:Item.ImageFile3 %>" style="border:solid; height:300px" alt="No Image"/>
                </td>
                <td>
                    <img src="<%#:Item.ImageFile4 %>" style="border:solid; height:300px" alt="No Image" />
                </td>
            </tr>
            <tr>
                <td style="vertical-align: top; text-align:left;">
                    <b>Author:</b><br /><%#:Item.Author %>
                    <br />
                    <span><b>Submit Date:</b>&nbsp;<%#: Item.SubmitDate %></span>
                    <br />
                    <span><b>Stem:</b>&nbsp;<%#:Item.Stem %></span>
                    <br />
                    </td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:FormView>
</asp:Content>

页面背后的当前代码:

namespace cs1.Submit
    {
    public partial class Submit_Detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    public IQueryable<Questions> GetQuestion([QueryString("QuestionID")] int? QuestionId)
    {
        var _db = new cs1.Models.ProductContext();
        IQueryable<Questions> query = _db.Questions;
        if (QuestionId.HasValue && QuestionId > 0)
        {
            query = query.Where(p => p.QuestionID == QuestionId);
        }
        else
        {
            query = null;
        }
        return query;
    }
}
}

SQL Server中的每个图像都有三列用于binaryData,ContentType和ImageName。

镜像文件, Image1Content, Image1Name, Image2File, Image2Content, Image2Name, 等

1 个答案:

答案 0 :(得分:1)

老实说,不确定这个链接和搜索有多少,但是这个链接ASP.NET images from SQL的文章能够快速轻松地处理我需要的情况。所有代码都在aspx页面中,没有(新)代码。这是用于提取图像的新aspx页面代码。谢谢大家的帮助:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
 <asp:FormView ID="Submit_tDetail" runat="server" ItemType="cs1.Models.Questions" SelectMethod ="GetQuestion" RenderOuterTable="false">
    <ItemTemplate>
        <div>
            <h1><%#:Item.KeyObjective %></h1>
        </div>
        <br />
        <table>
            <tr>
                <td>
                    <asp:Image id="Image1" AlternateText="No Image" runat="server" Height="200px" ImageUrl='<%# "data:image/jpg;base64,"+ Convert.ToBase64String((byte[])Eval("ImageFile")) %>' />
                </td>
                <td>
                    <asp:Image id="Image2" AlternateText="No Image" runat="server" Height="200px" ImageUrl='<%# "data:image/jpg;base64,"+ Convert.ToBase64String((byte[])Eval("ImageFile2")) %>' />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Image id="Image3" AlternateText="No Image" runat="server" Height="200px" ImageUrl='<%# "data:image/jpg;base64,"+ Convert.ToBase64String((byte[])Eval("ImageFile3")) %>' />
                </td>
                <td>
                    <asp:Image id="Image4" AlternateText="No Image" runat="server" Height="200px" ImageUrl='<%# "data:image/jpg;base64,"+ Convert.ToBase64String((byte[])Eval("ImageFile4")) %>' />
                </td>
            </tr>
            <tr>
                <td style="vertical-align: top; text-align:left;">
                    <b>Author:</b><br /><%#:Item.Author %>
                    <br />
                    <span><b>Submit Date:</b>&nbsp;<%#: Item.SubmitDate %></span>
                    <br />
                    <span><b>Stem:</b>&nbsp;<%#:Item.Stem %></span>
                    <br />
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:FormView>
</asp:Content>
相关问题