将图像从数据库绑定到datalist

时间:2013-07-18 12:19:18

标签: asp.net .net vb.net visual-studio-2008

我将图像绑定到datalist。

我的imagename在数据库中,我正在接受它并希望将它绑定到datalist。

我试过以下:

  <asp:DataList ID="dlImages" runat="server">
            <ItemTemplate>
               <asp:ImageButton ID="ImageButton1" Height="200px" Width="200px" 
  ImageUrl='<%# Bind ("PageName","D:\Sagar\Kinston\WebSite\ScreenMasterImages\{0}") %>' runat="server" />
            </ItemTemplate>
        </asp:DataList>

在pageload上我将它限制为:

ds = gc.GetDataToListBinder("select DISTINCT PageOrderID,PageName from ScreenMaster order by PageOrderID")
            dlImages.DataSource = ds.Tables(0)
            dlImages.DataBind()

上面的代码ds是我的数据集,gc.GetDataToListBinder(query)返回dataset

但图像没有显示出来。

可能是什么错误?

EDIT1:

<asp:ImageButton ID="ImageButton1" Height="200px" Width="200px" ImageUrl='<%#Server.HtmlDecode(Eval("PageName","D:\Sagar\Kinston\WebSite\ScreenMasterImages\{0}.jpg")) %>'  runat="server" />

4 个答案:

答案 0 :(得分:1)

请花一点时间阅读:

http://www.codeproject.com/Articles/142013/There-is-something-about-Paths-for-Asp-net-beginne

我认为这会对你有所帮助。

修改

对于太空问题,请看一下:

Why does HttpUtility.UrlEncode(HttpUtility.UrlDecode("%20")) return + instead of %20?

<强>基本上

ImageUrl='<%# Server.HtmlDecode(Bind("MyImage")) %>'

但我建议您在db中存储没有空格的图像名称。

<强> EDIT2:

ImageUrl='<%# Eval("MyImage") %>'

ImageUrl='<%# Server.HtmlDecode(Eval("MyImage")) %>'  

答案 1 :(得分:1)

<asp:DataList  ID="DataList1" runat="server" RepeatColumns="3" 
                                    RepeatDirection="Horizontal" CellPadding="2" 
                                    CellSpacing="2"
                                    UseAccessibleHeader="True" >
                                    <ItemTemplate>
                                    <asp:ImageButton Width="120" Height="120" ID="Image1" ImageUrl='<%# Eval("Imgpath", "") %>' runat="server" />
        </asp:Datalist>



    .CS

    SqlConnection con;
    SqlCommand cmd;
    string strCon = "Connection String";
    SqlDataAdapter da;

    protected void AlldataImg()
        {
            DataTable dt = new DataTable();
            string strQuery = "select code,imgpath from SA_Stock order by Code";
            cmd = new SqlCommand(strQuery);
            con = new SqlConnection(strCon);
            da = new SqlDataAdapter();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;

            try
            {
                con.Open();
                da.SelectCommand = cmd;
                da.Fill(dt);
                DataList1.DataSource = dt;
                DataList1.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                con.Close();
                da.Dispose();
                con.Dispose();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
                AlldataImg();
    }

    Put Image Path in database too so that image will appear..

答案 2 :(得分:1)

protected void DataListPosts_ItemDataBound(object sender, DataListItemEventArgs e)
{
    try
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRowView dr = (DataRowView)e.Item.DataItem;
            Image ImageData = (Image)e.Item.FindControl("ImageData");
            if (dr.Row[4].ToString() != "NA")
            {
                ImageData.Visible = true;
                ImageData.ImageUrl = @"ImgPost/" + dr.Row["ImgPath"].ToString();
            }
            else
                ImageData.Visible = false;


        }
    }
    catch (Exception)
    { }
}

答案 3 :(得分:1)

这是一个古老的主题,但我认为它对任何人都有用

首先从字符串中获取数据

string sqlget =“select from Promoter”;

并将其发送到数据库dsget

然后那样做

dsget.Tables [0] .Columns.Add(“Photopath”,typeof(string));

    for (int i = 0; i < dsget.Tables[0].Rows.Count; i++)
    {
        if (dsget.Tables[0].Rows[i]["Photo"].ToString() != null && dsget.Tables[0].Rows[i]["Photo"].ToString() != "")
        {
            var data = (Byte[])(dsget.Tables[0].Rows[i]["Photo"]);
            var stream = new MemoryStream(data);
            System.IO.BinaryReader br = new System.IO.BinaryReader(stream);
            FileBytes = br.ReadBytes((Int32)stream.Length);
            string base64String = Convert.ToBase64String(FileBytes, 0, FileBytes.Length);
            dsget.Tables[0].Rows[i]["Photopath"] = "data:image/png;base64," + base64String;
        }
    }

    DataList1.DataSource = dsget.Tables[0];
    DataList1.DataBind();

在asp文件中写下以下内容