Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Listview ItemTemplate中生成文本作为图像_C#_Asp.net - Fatal编程技术网

C# 如何在Listview ItemTemplate中生成文本作为图像

C# 如何在Listview ItemTemplate中生成文本作为图像,c#,asp.net,C#,Asp.net,我有一个列表视图,我想在其中生成我的电子邮件数据作为图像。下面是我的代码 <%@ Import Namespace="System.Drawing" %> <%@ Import Namespace="System.Drawing.Text" %> <%@ Import Namespace="System.Drawing.Imaging" %> <%@ Import Namespace="System.Drawing.Drawing2D" %>

我有一个列表视图,我想在其中生成我的电子邮件数据作为图像。下面是我的代码

<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Text" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2D" %>

 <asp:ListView ID="lvtest" runat="server" 
      CellPadding="0" 
      RepeatDirection="Horizontal" 
      GroupPlaceholderID="itemPlaceholdersp" 
      ItemPlaceholderID="itemPlaceholdersp" >
      <LayoutTemplate>
           <asp:PlaceHolder ID="itemPlaceholdersp" runat="server"></asp:PlaceHolder>
      </LayoutTemplate>
      <ItemTemplate>
           <div class="accordionBg">                              
                <h3><%# Eval("UserName") %></h3>
                <h4>
                     <%# Eval("Designation").ToString() == "" ? "" : Eval("Designation") + "<br/>" %>
                     <%# Eval("Qualification").ToString() == "" ? "" : Eval("Qualification")+ "<br/>" %>

                     <%
                          Response.Clear();
                          int height = 100;
                          int width = 200;
                          Random r = new Random();
                          int x = r.Next(75);

                          Bitmap bmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);
                          Graphics g = Graphics.FromImage(bmp);

                          g.TextRenderingHint = TextRenderingHint.AntiAlias;
                          g.Clear(Color.Orange);
                          g.DrawRectangle(Pens.White, 1, 1, width-3, height-3);
                          g.DrawRectangle(Pens.Gray, 2, 2, width-3, height-3);
                          g.DrawRectangle(Pens.Black, 0, 0, width, height);
                          g.DrawString(Eval("Email").ToString(), new Font("Arial", 12, FontStyle.Italic), 
                          SystemBrushes.WindowText, new PointF(x,50) );

                          bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
                          g.Dispose();
                          bmp.Dispose();
                          Response.End();
                      %>
                </h4>

                <p class="clear"></p>
            </div>

            <div class="accordion_content">
                <div class="vertical_accordion_content">
                    <p><%# Eval("Description")%></p>
                </div>
            </div>
        </div>
    </ItemTemplate>
</asp:ListView>

请帮助我。

您描述的错误不是由渲染代码引起的,而是由于缺少列表视图的有效数据源。我把你的代码简化了一点。它应该会导致相同的错误(如果我们查看的是实际的标记):

请注意,上述操作可能没有任何作用,并且可能会导致其他错误。有关更多详细信息,您可以参考或浏览web上的5个教程之一

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
<asp:ListView ID="lvtest" runat="server">
    <ItemTemplate>
        <h3><%# Eval("UserName") %></h3>
    </ItemTemplate>
</asp:ListView>
<asp:ObjectDataSource ID="MyDataSource" runat="server"></asp:ObjectDataSource>

<asp:ListView ID="lvtest" runat="server" DataSourceID="MyDataSource">
    <ItemTemplate>
        <h3><%# Eval("UserName") %></h3>
    </ItemTemplate>
</asp:ListView>