0

我正在将图像绑定到数据列表。

我的图像名在数据库中,我正在使用它并想将它绑定到数据列表。

我试过以下:

  <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>

在页面加载上,我将其限制为:

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

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

但是图像没有显示出来。

可能是什么错误?

编辑1:

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

4 回答 4

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..
于 2014-05-24T09:20:23.493 回答
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)
    { }
}
于 2014-05-24T09:48:42.020 回答
1

花一点时间阅读以下内容:

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

我认为这会对你有很大帮助。

编辑:

对于空间问题,请看:

为什么 HttpUtility.UrlEncode(HttpUtility.UrlDecode("%20")) 返回 + 而不是 %20?

基本上:

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

但我建议您将图像名称存储在 db 中,不要使用空格。

编辑2:

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

ImageUrl='<%# Server.HtmlDecode(Eval("MyImage")) %>'  
于 2013-07-18T12:42:39.137 回答
1

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

首先从字符串中获取数据

string sqlget = "从促销员中选择照片";

并将其发送到数据库 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文件中写入以下内容

于 2017-08-05T17:01:43.187 回答