本文介绍了如何将图像绑定到gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 我使用文件上传来获取图像,并使用字节数组将其保存到数据库(Sql server)中。我的图像数据类型ID图像。 我使用一个asp处理程序将图像绑定到gridview。 但是在执行时处理程序给出错误image_id是无效的列。 ************** 我的代码是 .aspx文件 在gridview中我的图像字段是 < asp :TemplateField > < ItemTemplate > < asp:Image runat = server ID = grdvwimg ImageUrl =' <%# ModelHandler.ashx?id = + Eval( mdl_id)%> ' 高度 = 150px 宽度 = 150px / > < / ItemTemplate > ********* .cs public void bind() { cmd = new SqlCommand (); con = new SqlConnection(strConnString); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue(@ Flag,13); cmd.CommandText =StoreProcedureSelect; cmd.Connection = con; 尝试 { con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); grdvwModel.DataSource = ds; grdvwModel.DataBind(); } catch(exception ex) { throw ex; } } ********************* *** hamdler.ashx < pre> public void ProcessRequest(HttpContext context) { try { con = new SqlConnection(strConnString); string imageid = context.Request.QueryString [id]; SqlCommand command = new SqlCommand(SELECT mdlimg FROM Model WHERE mdl_id ='imageid',con); con.Open(); SqlDataReader dr = command.ExecuteReader(); dr.Read(); context.Response.BinaryWrite((Byte [])dr [0]); context.Response.End(); } catch(exception ex) { throw ex; } } public bool IsReusable { get { return false; } } } 解决方案 尝试 ASP.NET 2.0的GridView示例:在GridView列中显示图像 [ ^ ] 将图像绑定到asp.net中的gridview [ ^ ] Hello everyone,I take image using file upload and save it into database (Sql server)by using byte array. My image datatype id image.I take one asp handler to bind image to gridview.But at the time of execution the handler give error image_id is not valid column.**************My code is.aspx fileIn gridview my image field is<asp:TemplateField> <ItemTemplate> <asp:Image runat="server" ID="grdvwimg" ImageUrl='<%#"ModelHandler.ashx?id="+Eval("mdl_id")%>' Height="150px" Width="150px" /> </ItemTemplate>*********.cs public void bind() { cmd = new SqlCommand(); con = new SqlConnection(strConnString); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Flag", 13); cmd.CommandText = "StoreProcedureSelect"; cmd.Connection = con; try { con.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); grdvwModel.DataSource = ds; grdvwModel.DataBind(); } catch (Exception ex) { throw ex; } }************************hamdler.ashx <pre> public void ProcessRequest(HttpContext context) { try { con = new SqlConnection(strConnString); string imageid = context.Request.QueryString["id"]; SqlCommand command = new SqlCommand("SELECT mdlimg FROM Model WHERE mdl_id = 'imageid'", con); con.Open(); SqlDataReader dr = command.ExecuteReader(); dr.Read(); context.Response.BinaryWrite((Byte[])dr[0]); context.Response.End(); } catch (Exception ex) { throw ex; } } public bool IsReusable { get { return false; } } } 解决方案 TryGridView Examples for ASP.NET 2.0: Displaying Images in a GridView Column[^]Binding image into gridview in asp.net[^] 这篇关于如何将图像绑定到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 21:07