ASP.net从SQL显示图像

时间:2012-03-27 08:12:46

标签: c# asp.net sql image

解决:我使用过处理程序。谢谢顺便说一下。 http://www.aspdotnetcodes.com/Insert_Images_Database.aspx

如何在DataList1中显示我的图像? 我对数据绑定等没有明确的想法。我希望你们能帮忙。谢谢 这是我的ASPX代码

<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
    <ItemTemplate>
        itemID:
        <asp:Label ID="itemIDLabel" runat="server" Text='<%# Eval("itemID") %>' />
        <br />
        Cloth ID:
        <asp:Label ID="Cloth_IDLabel" runat="server" Text='<%# Eval("[Cloth ID]") %>' />
        <br />
        Style:
        <asp:Label ID="StyleLabel" runat="server" Text='<%# Eval("Style") %>' />
        <br />
        Size:
        <asp:Label ID="SizeLabel" runat="server" Text='<%# Eval("Size") %>' />
        <br />
        Color:
        <asp:Label ID="ColorLabel" runat="server" Text='<%# Eval("Color") %>' />
        <br />
        Image 1:
        <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("[Image 1]") %>' />
        <br />
        Image 2:
        <asp:Image ID="Image2" runat="server" ImageUrl='<%# Eval("[Image 2]") %>' />
        <br />
        Price:
        <asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>' />
        <br />
        Notes:
        <asp:Label ID="NotesLabel" runat="server" Text='<%# Eval("Notes") %>' />
        <br />
        Alignment of Image 1:
        <asp:Label ID="Alignment_of_Image_1Label" runat="server" 
            Text='<%# Eval("[Alignment of Image 1]") %>' />
        <br />
        Alignment of Image 2:
        <asp:Label ID="Alignment_of_Image_2Label" runat="server" 
            Text='<%# Eval("[Alignment of Image 2]") %>' />
            <br />
    </ItemTemplate>
</asp:DataList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:TPSConnectionString %>" 
    SelectCommand="SELECT CustomizedOrder.userid, CustomizedOrder.itemID, ClothInventory.clothID AS [Cloth ID], ClothInventory.style AS Style, ClothInventory.size AS Size, Color.color AS Color, CustomizedOrder.image1 AS [Image 1], CustomizedOrder.image2 AS [Image 2], CustomizedOrder.itemPrice AS Price, CustomizedOrder.notes AS Notes, Alignment.description AS [Alignment of Image 1], Alignment_1.description AS [Alignment of Image 2] FROM CustomizedOrder INNER JOIN ClothInventory ON CustomizedOrder.clothID = ClothInventory.clothID INNER JOIN Color ON ClothInventory.colorID = Color.colorID INNER JOIN Alignment ON CustomizedOrder.alignment1 = Alignment.alignmentID INNER JOIN Alignment AS Alignment_1 ON CustomizedOrder.alignment2 = Alignment_1.alignmentID WHERE (CustomizedOrder.userid = @userid)">
    <SelectParameters>

    </SelectParameters>
</asp:SqlDataSource>

这是针对aspx.cs

public partial class addtoShoppingCart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MembershipUser User = Membership.GetUser();
        object UserGUID = User.ProviderUserKey;
        SqlDataSource1.SelectParameters.Add("userid", UserGUID.ToString());
        SqlDataSource1.DataBind();
    }
}

3 个答案:

答案 0 :(得分:0)

Asp:Image.ImageUrl需要一个URL值,因此如果Image1是一个字符串,其中包含您的代码应该运行的图像的URL。

相反,如果Image1是图像本身,那么您需要在服务器上暂时save it并提供带有保存文件的URL的Asp:Image控件(选中this example用于生成URL)

答案 1 :(得分:0)

如果您没有来自数据库的图片网址,并且您的sql中有图像数据类型,则此代码将有助于generate image from sql database

答案 2 :(得分:0)

如果图像路径存储在数据库中:〜/ Images / file-name.jpg

=&GT; datalist中的图片网址:

ImageUrl='<%# Eval("[ImagePath]") %>' />