在Datalist中显示Mysql数据库中的图像

时间:2013-02-27 13:30:44

标签: c# asp.net mysql

我在myqsl数据库中保存图像路径和图像名称。现在我想在asp.net数据列表中显示数据库中的图像。我有这样的查询结果 enter image description here

我想用pageName显示图片。我在datalst做了类似的事情。

<asp:DataList ID="dtlistImages" runat="server" RepeatColumns="3" RepeatDirection="Horizontal"
                        BorderColor="#336699" BorderStyle="Solid" BorderWidth="2px">
            <ItemTemplate>
                <asp:Label ID="lblID" runat="server" Text='<%# Eval("pageName") %>' Font-Bold="true"
                            Font-Size="10pt" ForeColor="#336699" Width="100%"/>
                            </br>
                 <asp:Image ID="imgnewspaper" runat="server" ImageUrl='<%#  Eval("pageNumber") %>' />
                 </ItemTemplate>
                 <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />    
    </asp:DataList>

代码背后:

  public DataTable getData(string query)
        {
            MySqlConnection conn1 = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
            conn1.Open();
            MySqlCommand cmd = new MySqlCommand(query, conn1);
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
        protected void btnshow_onclick(object sender, EventArgs e)
        {
            string query = "select cap_newspaper_page.pageServer,cap_newspaper_page.pagePath, pageNumber,pageName,newspaper.newspaperName,newspaper_station.newspaperStationName"+
                            " FROM cap_newspaper_page Inner Join cap_newspaper ON cap_newspaper_page.capnewspaperID = cap_newspaper.capnewspaperID inner join newspaper on newspaper.newspaperID=cap_newspaper.newspaperID"+
                            " inner join newspaper_station on newspaper_station.newspaperStationID=cap_newspaper.newspaperStationID where cap_newspaper.newspaperID="+ddlNewspaper.SelectedValue+" and cap_newspaper.newspaperStationID="+ddlNewspaperStation.SelectedValue+" and cap_newspaper_page.publishDate='"+tbDate.Text+" 00:00:00'";
            dtlistImages.DataSource = getData(query);           
            dtlistImages.DataBind();
        }

但显示是这样的 enter image description here

我如何动态显示图像。基于查询。

3 个答案:

答案 0 :(得分:1)

编译的html的源代码是什么样的?很明显,图像路径必须无效。如果你检查一下asp写了什么,这不能给出一个有关错误的强有力线索吗?

答案 1 :(得分:1)

您的Eval中只有pageNumber而不是pagePath

答案 2 :(得分:0)

<asp:Image ID="imgnewspaper" runat="server" ImageUrl='<%#  Eval("pagePath") %>' />


如果您在本地运行它,则在本地丢失项目文件夹printImages中的图像,如果您在实时服务器上运行它,则还要检查该文件夹中的图像。

建议我通过以下方式实现了同样的目标:

    <asp:ListView ID="ImagesList" runat="server" DataKeyNames="ID" GroupItemCount="4"  OnPagePropertiesChanging="ImagesList_PagePropertiesChanging">
        <EmptyDataTemplate>
            No Images found.
         </EmptyDataTemplate>
          <ItemTemplate>
               <asp:ImageButton ID="MyPicture" runat="server" OnClick="MyPicture_Click" CommandArgument='<%# Eval("ID") %>'  AlternateText='<%# Eval("Name") %>' ImageUrl='<%# Eval("ImagePath") %>' Autopostback="true"Width="155" Height="80" />
  </ItemTemplate>
</asp:ListView>