c# - 数据绑定(bind)误解

标签 c# asp.net

我有一个问题,我需要你的帮助:

我正在制作一个 Web 应用程序来访问我需要在类似( ListView 或 DataList )这样的工具中显示他们的员工列表,以直接绑定(bind)数据库中的数据,并且数据库中有一个字段保存为图像我需要用我在这里提到的细节来展示这张图片 enter image description here

然后我创建一个 ASP 处理程序来流式传输图像,它采用查询字符串(“代码”),它的返回字节流包括“data:image/png;base64,image”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TimeSheet.DstTableAdapters;

namespace TimeSheet
{
    /// <summary>
    /// Summary description for emp_img
    /// </summary>
    public class emp_img : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string emp_code = context.Request.QueryString["code"];
            string output_img = "";
            string no_image = "i remove the image code from here ";
            string male = "i remove the image code from here ";

            if (emp_code == null || emp_code == "")
            {
                output_img = no_image;
            }
            else
            {

                try
                {
                    EmployeeTableAdapter adp = new EmployeeTableAdapter();
                    Dst.EmployeeDataTable all_emps = adp.get_by_code(emp_code);
                    foreach (Dst.EmployeeRow emp in all_emps)
                    {
                        if (emp.Data == null)
                        {
                            //here to add the users without image 
                            output_img = male;
                        }
                        else
                        {
                            byte[] bytes = emp.Data;
                            string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
                            output_img = "data:image/png;base64," + base64String;
                        }


                    }
                }
                catch (Exception)
                {

                    output_img = no_image;
                }



                context.Response.BinaryWrite(GetBytes(output_img));
                context.Response.End();

            }

        }

然而,当我尝试在我的代码中以这种方式使用它时......

        <asp:ListView ID="ListView1" runat="server" DataSourceID="myteamdata" ItemPlaceholderID="placeholder" OnItemInserting="ListView1_ItemInserting" >
            <LayoutTemplate>
                <div class="row">
                <div class="col-lg-12" id="placeholder" runat="server">
                </div>
                    </div>
                <asp:DataPager ID="DataPager1" runat="server" PageSize="12"></asp:DataPager>
            </LayoutTemplate>

            <ItemTemplate>
                <div class="col-lg-3">

                    <asp:Image ID="Image1" runat="server" ImageUrl='<%# "emp_img.ashx?code="+ Eval("Code") %>'/>

                </div>
            </ItemTemplate>

        </asp:ListView>



        <asp:ObjectDataSource ID="myteamdata" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="get_all_data" TypeName="TimeSheet.DstTableAdapters.EmployeeTableAdapter"></asp:ObjectDataSource>

它并没有插入图片,它只是保留了一个 url 之类的 enter image description here

那么解决方案是什么来处理来自数据绑定(bind)的输出,例如 c# 代码中的(bind 或 Eval),以及我如何调用方法在某些类中进行一些计算到使用该数据绑定(bind)的 ASP 页面中结果作为该方法的输入

非常感谢您的提前帮助

最佳答案

你的图像处理器返回的是一个字符串,

   byte[] bytes = emp.Data;
   string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
   output_img = "data:image/png;base64," + base64String;

但是img可以显示的是一个真正的二进制文件/数据

将您的内容类型更改为

context.Response.ContentType = "image/png";
context.Response.BinaryWrite(emp.Data);

并按原样发送您的数据。

你有更多的错误,你不存在的图像必须是磁盘上的图像文件......我不知道还有什么......返回图像的 foreach 也不好如果你找到了不止一张图片...

关于c# - 数据绑定(bind)误解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37641714/

相关文章:

asp.net - 从服务调用函数时出现"Current user did not login to the application!"错误

c# - .OrderBy 函数的 System.Linq.Expressions.Expression

c# - MVC ActionLink 将路由附加为查询字符串而不是路由值

sql - SQL中如何减去数值范围?

asp.net - 我想将 Gmap 控件中的纬度和经度获取到文本框中

c# - 获取 WCF 响应的内容类型

c# - 在 Button_Click 上围绕控件绘制边框

c# - 如何将最后插入的 id 转换为字符串?

c# - C# 中的环境变量

c# - 从 (GAC) 类库初始化 Automapper