本文介绍了在使用处理程序时,图像没有正确绑定到datalist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在尝试将图像绑定到datalist,我面临的问题是它没有得到绑定或处理程序文件没有被执行。告诉我这方面的简单解决方案或请给我简单的链接以供参考。我知道它很容易但不知道我做错了什么 < asp:DataList ID = DataList1 runat = server BackColor = #DEBA84 BorderColor = #DEBA84 BorderStyle = 无 BorderWidth = 1px CellPadding = 3 CellSpacing = 2 可见 = False OnItemDat aBound = DataList1_ItemDataBound 字体名称 = Times New Roman 字体 - 大小 = 更大 字体粗体 = 错误 GridLines = Both RepeatColumns = 1 RepeatDirection = 水平 宽度 = 960px > < HeaderStyle BackColor = #A55129 字体大小 = 大 ForeColor = 白色 HorizontalAlign = 中心 VerticalAlign = 中间 / > QueNo: < asp:标签 ID = QueNoLabel runat = server 文字 =' <% #Eval( QueNo) %>' / > QuestionText: < asp:Label ID = QuestionTextLabel runat = server 文字 =' <% #Eval( QuestionText)%>' > QuestionType:< asp:Label ID = QuestionTypeLabel 文本 =' <% #Eval( QuestionType)%>' runat = server > 选项1: < asp:标签 ID = Option1Label runat = server 文本 =' <% #Eval( Option1)%>' > 选项2:< asp:标签 ID = Option2Label 文字 =' <% #Eval( Option2)%>' runat = 服务器 > 选项3: < asp:标签 ID = Option3Label runat = server 文字 =' <% #Eval( Option3)%>' > 选项4:< asp:Label ID = Option4Label 文本 =' <% #Eval( Option4) %GT;' runat = server > RightMark: < asp:Label ID = RightMarkLabel runat = server 文本 =' <% #Eval( RightMark)%>' / > NegativeMark: < asp:Label ID = NegativeMarkLabel runat = server 文字 =' <% #Eval( NegativeMark)%>' / > < asp:图像 ID = imgemp runat = server ImageUrl =' <%#DataBinder.Eval (Container.DataItem, images)%>' / > <% - QuestionImage:< ; ASP :Image ID =imgEmprunat =serverWidth =100pxHeight =120px style =padding-left:40px/> <% - < img src =GridViewImage123.ashx?autoid =<%#Eval(Id)。ToString()%> & tableName =<%#Eval(TableName)。ToString()%> - %> < SelectedItemStyle BackColor =#738A9CFont-Bold =真ForeColor =白色/> string connString = ConfigurationManager.ConnectionStrings [ QuestionExam]。ConnectionString; DataTable dsProducts = new DataTable(); // 打开SQL连接 using (SqlConnection conn = new SqlConnection(connString)) { conn.Open(); // 初始化命令对象 使用(SqlCommand cmd = new SqlCommand( QuestionPaper1,conn)) { string session =( string )会话[ SubjectName]; cmd.CommandType = CommandType.StoredProcedure; // 将输入参数添加到SP cmd.Parameters.AddWithValue( @ Subjectname,session); SqlDataAdapter adapter = new SqlDataAdapter(cmd); // 填写结果集 adapter.Fill(dsProducts); } } DataColumn imageCol = new DataColumn( images, typeof ( string )); dsProducts.Columns.Add(imageCol); if (dsProducts.Rows.Count > 0 ) { for ( int i = 0 ; i < dsProducts.Rows.Count; i ++) { 尝试 { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DataRowView dr =(DataRowView)e.Item.DataItem; Image ImageData =(Image)e.Item.FindControl( imgemp); ImageData.ImageUrl = Page.ResolveUrl( 〜/ ImgHandler.ashx?QueNo = + dsProducts.Rows [i] [ QueNo] + ); ImageData.Visible = true ; } } catch (例外) {} } } } 处理程序 public void ProcessRequest(HttpContext context){ System.Data.SqlClient.SqlDataReader rdr = null ; System.Data.SqlClient.SqlConnection conn = null ; System.Data.SqlClient.SqlCommand selcmd = null ; 尝试 { string sess =( string )context.Session [ SubjectName]。ToString (); string section =( string )context.Session [ SectionName]; string id = HttpContext.Current.Request.QueryString [ QueNo]; conn = new System.Data.SqlClient.SqlConnection (System.Configuration.ConfigurationManager.ConnectionStrings [ QuestionExam]。ConnectionString); selcmd = new System.Data.SqlClient.SqlCommand // (选择*来自Upimage,其中ContestName ='+ HttpContext.Current.Request.Cookies [Contest]。Value +' // ,conn); ( select * from V_QuestionPaper where SubjectName =' + HttpContext.Current.Session [ SubjectName] + '和QueNo = + id + ,conn); conn.Open(); rdr = selcmd.ExecuteReader(); while (rdr.Read()) { context.Response.ContentType = image / jpg; context.Response.BinaryWrite(( byte [])rdr [ QuestionImage]); System.IO.MemoryStream stream = new System.IO.MemoryStream(( byte [] )rdr [ QuestionImage], true ); stream.Write(( byte [])rdr [ QuestionImage], 0 , 100 ); // 从流中创建位图 系统。 Drawing.Bitmap bmp = new System.Drawing.Bitmap(stream); System.Drawing.Size new_size = new System.Drawing.Size(); int outputSize = 100 ; // 根据较长的尺寸调整大小 if (bmp.Width > bmp.Height) { new_size.Width = outputSize; new_size.Height =( int )((( double )outputSize /( double )bmp.Width)*( double )bmp.Height); } else { new_size.Width =( int (( double )outputSize /( double )bmp.Height)*(双)bmp.Width); new_size.Height = outputSize; } System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(new_size.Width,new_size.Height,bmp.PixelFormat); System.Drawing.Graphics new_g = System.Drawing.Graphics.FromImage(bitmap); new_g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; new_g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; new_g.DrawImage(bmp,-1,-1,bitmap.Width + 1 ,bitmap.Height + 1 ); bmp.Dispose(); // 将bitmapt绘制到输出流 位图。保存(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg); bitmap.Dispose(); new_g.Dispose(); // 关闭流 stream.Close() ; } } catch (例外情况) {} 最后 { if (conn!= null ) conn.Close(); } 解决方案 你的事情很复杂。要么使用Handler,要么直接从DataItem中获取图像URL。 请参阅 - [ ^ ] 2. 在DataList控件中显示数据库中的图像 [ ^ ] 3. 图像处理T [ ^ i am trying to bind the images to datalist and the problem i am facing is that either its not getting binded or handler file is not not getting executed. tell me simple solution for this or please give me simple link for reference .i know its very easy but don't know what i am doing wrong<asp:DataList ID="DataList1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84"BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Visible="False"OnItemDataBound="DataList1_ItemDataBound"Font-Names="Times New Roman" Font-Size="Larger" Font-Bold="False" GridLines="Both" RepeatColumns="1"RepeatDirection="Horizontal" Width="960px"><HeaderStyle BackColor="#A55129" Font-Size="Large" ForeColor="White"HorizontalAlign="Center" VerticalAlign="Middle" />QueNo:<asp:Label ID="QueNoLabel" runat="server" Text='<%# Eval("QueNo") %>' />QuestionText:<asp:Label ID="QuestionTextLabel" runat="server"Text='<%# Eval("QuestionText") %>'>QuestionType: <asp:Label ID="QuestionTypeLabel"Text='<%# Eval("QuestionType") %>' runat="server" >Option1:<asp:Label ID="Option1Label" runat="server" Text='<%# Eval("Option1") %>'>Option2: <asp:Label ID="Option2Label" Text='<%# Eval("Option2") %>'runat="server">Option3:<asp:Label ID="Option3Label" runat="server" Text='<%# Eval("Option3") %>'>Option4: <asp:Label ID="Option4Label" Text='<%# Eval("Option4") %>'runat="server">RightMark:<asp:Label ID="RightMarkLabel" runat="server" Text='<%# Eval("RightMark") %>' />NegativeMark:<asp:Label ID="NegativeMarkLabel" runat="server"Text='<%# Eval("NegativeMark") %>' /><asp:Image ID="imgemp" runat="server" ImageUrl=' <%#DataBinder.Eval(Container.DataItem, "images") %>' /><%-- QuestionImage:<asp:Image ID="imgEmp" runat="server" Width="100px" Height="120px"style="padding-left:40px"/><%-- <img src="GridViewImage123.ashx?autoid=<%# Eval("Id").ToString() %>&tableName=<%# Eval("TableName").ToString() %>"--%><SelectedItemStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />string connString =ConfigurationManager.ConnectionStrings["QuestionExam"].ConnectionString;DataTable dsProducts = new DataTable();//Open SQL Connectionusing (SqlConnection conn = new SqlConnection(connString)){conn.Open();//Initialize command objectusing (SqlCommand cmd = new SqlCommand("QuestionPaper1", conn)){string session = (string)Session["SubjectName"];cmd.CommandType = CommandType.StoredProcedure;//add input parameters to the SPcmd.Parameters.AddWithValue("@Subjectname", session);SqlDataAdapter adapter = new SqlDataAdapter(cmd);//Fill the result setadapter.Fill(dsProducts);}}DataColumn imageCol = new DataColumn("images", typeof(string));dsProducts.Columns.Add(imageCol);if (dsProducts.Rows.Count > 0){for (int i = 0; i < dsProducts.Rows.Count; i++){try{if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem){DataRowView dr = (DataRowView)e.Item.DataItem;Image ImageData = (Image)e.Item.FindControl("imgemp");ImageData.ImageUrl = Page.ResolveUrl("~/ImgHandler.ashx?QueNo=" + dsProducts.Rows[i]["QueNo"] + "");ImageData.Visible = true;}}catch (Exception){ }}}}in handlerpublic void ProcessRequest (HttpContext context) {System.Data.SqlClient.SqlDataReader rdr = null;System.Data.SqlClient.SqlConnection conn = null;System.Data.SqlClient.SqlCommand selcmd = null;try{string sess = (string)context.Session["SubjectName"].ToString();string section = (string)context.Session["SectionName"];string id = HttpContext.Current.Request.QueryString["QueNo"];conn = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["QuestionExam"].ConnectionString);selcmd = new System.Data.SqlClient.SqlCommand//("select * from Upimage where ContestName = '"+HttpContext.Current.Request.Cookies["Contest"].Value+"'"//, conn);("select * from V_QuestionPaper where SubjectName = '" + HttpContext.Current.Session["SubjectName"] + "' and QueNo=" + id + "", conn);conn.Open();rdr = selcmd.ExecuteReader();while (rdr.Read()){context.Response.ContentType = "image/jpg";context.Response.BinaryWrite((byte[])rdr["QuestionImage"]);System.IO.MemoryStream stream = new System.IO.MemoryStream((byte[])rdr["QuestionImage"], true);stream.Write((byte[])rdr["QuestionImage"], 0, 100);// Create a bitmap from the streamSystem.Drawing.Bitmap bmp = new System.Drawing.Bitmap(stream);System.Drawing.Size new_size = new System.Drawing.Size();int outputSize = 100;//resize based on the longer dimensionif (bmp.Width > bmp.Height){new_size.Width = outputSize;new_size.Height = (int)(((double)outputSize / (double)bmp.Width) * (double)bmp.Height);}else{new_size.Width = (int)(((double)outputSize / (double)bmp.Height) * (double)bmp.Width);new_size.Height = outputSize;}System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(new_size.Width, new_size.Height, bmp.PixelFormat);System.Drawing.Graphics new_g = System.Drawing.Graphics.FromImage(bitmap);new_g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;new_g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;new_g.DrawImage(bmp, -1, -1, bitmap.Width + 1, bitmap.Height + 1);bmp.Dispose();//Draw the bitmapt to the outputstreambitmap.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);bitmap.Dispose();new_g.Dispose();// Close the streamstream.Close();}}catch (Exception ex){ }finally{if (conn != null)conn.Close();} 解决方案 You are complicating things. Either use Handler, else bing the Image URL directly from DataItem.See - How to display images in DataList control using ASP.NET with C# - See more at: http://www.dotnetfox.com/articles/how-to-display-images-in-datalist-control-using-Asp-Net-with-C-Sharp-1037.aspx#sthash.nZCnHzdy.dpuf[^]2. Display Images From Database in DataList Control[^]3. Image Handling In ASP.NET[^] 这篇关于在使用处理程序时,图像没有正确绑定到datalist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-01 16:33