想要根据SQL值显示图像?

时间:2011-12-01 16:15:55

标签: c# asp.net .net sql

希望有人在这里可以帮助我!在gridview中,我有Field Attachment,取值来自SQL。我正在尝试评估附件字段,如果它包含文本,则显示附件Img,否则 - 没有或NoAttch.jpg。

我尝试创建ASP图像并使用后面的代码评估img但不显示图像。 任何帮助,让我疯了。

 <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="BookingsSQL"
 ShowHeader="False" Width="80%" BackColor="Azure" GridLines="Horizontal" 
        DataKeyNames="ID" AllowSorting="True" HorizontalAlign="Center" AutoGenerateDeleteButton="false">
 <Columns>
      <asp:TemplateField>
           <ItemTemplate>
                <table id="Table1" runat="server" visible="<%# Container.DataItemIndex == 0 %>">
                     <tr>
                          <th width="20%">
                               <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Sort" CommandArgument="programme_name" >
                                    Programme Name
                               </asp:LinkButton>
                          </th>
                          <th width="10%">
                               <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Sort" CommandArgument="Start">
                                    Start
                               </asp:LinkButton>

                          </th>
                          <th width="10%">
                               <asp:LinkButton ID="LinkButton3" runat="server" CommandName="Sort" CommandArgument="Finish">
                                    Finish
                               </asp:LinkButton>
                          </th>
                          <th width="20%">
                               <asp:LinkButton ID="LinkButton4" runat="server" CommandName="Sort" CommandArgument="Source">
                                    Source
                               </asp:LinkButton>
                          </th>
                          <th width="20%">
                               <asp:LinkButton ID="LinkButton5" runat="server" CommandName="Sort" CommandArgument="Destination">
                                    Destination
                               </asp:LinkButton>
                          </th>
                          <th width="40%">
                               <asp:LinkButton ID="LinkButton6" runat="server" CommandName="Sort" CommandArgument="BComment">
                                    Comment
                               </asp:LinkButton>
                          </th>
                          <th width="10%">
                               <asp:LinkButton ID="LinkButton15" runat="server" CommandName="Sort" CommandArgument="Attachment">
                                    Attachment
                               </asp:LinkButton>

                          </th>
                     </tr>
                </table>
                <asp:Panel runat="server" ID="ItemContainer">
                     <table width="80%">
                          <tr>
                               <td width="10%">
                                    <%# Eval("programme_name") %>
                               </td>
                               <td width="10%">
                                    <%# Eval("Start") %>
                               </td>
                               <td width="10%">
                                    <%# Eval("Finish") %>
                               </td>
                               <td width="20%">
                                    <%# Eval("Source") %>
                               </td>
                               <td width="20%">
                                    <%# Eval("Destination") %>
                               </td>
                               <td width="40%">
                                    <%# Eval("BComment") %>
                               </td>
                                 <td width="10%">
                                    <%# Eval("Attachment") %>
                               </td>
                          </tr>
                     </table>
                </asp:Panel>
                <asp:Panel CssClass="popupMenu" ID="PopupMenu" runat="server">
                     <asp:LinkButton ID="LinkButton7" runat="server" CommandName="Edit" Text="Edit" />
                     <br />
                     <asp:LinkButton ID="LinkButton8" runat="server" CommandName="Insert" CausesValidation="true"
                          Text="Insert" />
                     <br />
                       <asp:LinkButton ID="LinkButton16" runat="server" CommandName="Delete" CausesValidation="true"
                          Text="Delete" />
                </asp:Panel>
                <ajaxToolkit:HoverMenuExtender runat="server" TargetControlID="ItemContainer" PopupControlID="popupMenu"
                     HoverCssClass="popupHover" PopupPosition="Left" OffsetX="0" OffsetY="0" PopDelay="20">
                </ajaxToolkit:HoverMenuExtender>
           </ItemTemplate>
           <EditItemTemplate>
                <table id="Table1" runat="server" visible="<%# Container.DataItemIndex == 0 %>">
                     <tr>
                          <th width="10%">
                               <asp:LinkButton ID="LinkButton9" runat="server" CommandName="Sort" CommandArgument="programme_name">
                                    Programme Name
                               </asp:LinkButton>
                          </th>
                          <th width="10%">
                               <asp:LinkButton ID="LinkButton10" runat="server" CommandName="Sort" CommandArgument="Start">
                                    Start
                               </asp:LinkButton>
                          </th>
                          <th width="10%">
                               <asp:LinkButton ID="LinkButton11" runat="server" CommandName="Sort" CommandArgument="Finish">
                                    Finish
                               </asp:LinkButton>
                          </th>
                          <th width="20%">
                               <asp:LinkButton ID="LinkButton12" runat="server" CommandName="Sort" CommandArgument="Source">
                                    Source
                               </asp:LinkButton>
                          </th>
                          <th width="20%">
                               <asp:LinkButton ID="LinkButton13" runat="server" CommandName="Sort" CommandArgument="Destination">
                                    Destination
                               </asp:LinkButton>
                          </th>
                          <th width="40%">
                               <asp:LinkButton ID="LinkButton14" runat="server" CommandName="Sort" CommandArgument="BComment">
                                    Comment
                               </asp:LinkButton>
                          </th>
                           <th width="40%">


                               <asp:LinkButton ID="LinkButton17" runat="server" CommandName="Sort" CommandArgument="Attachment">
                                    Attachment
                               </asp:LinkButton>
                          </th>
                     </tr>
                </table>
            <!-- Add comments -->
                <asp:Panel runat="server" ID="ItemContainer">
                     <asp:HiddenField runat="server" ID="IdHiddenField" Value='<%# Bind("ID") %>' />
                     <table width="100%">
                          <tr>
                               <td width="10%">
                                    <asp:TextBox ID="TextBox0" runat="server" Text='<%# Bind("programme_name") %>' />
                               </td>
                               <td width="10%">
                                 <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Start") %>'  />
                               </td>
                               <td width="10%">
                                     <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Finish") %>'  />
                               </td>
                               <td width="20%">
                                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Source") %>'  />
                               </td>
                               <td width="20%">
                                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Destination") %>' />
                               </td>
                               <td width="40%">
                                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("BComment") %>' />
                               </td>
                                <td width="10%">
                                  <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Attachment") %>' />
                               </td>
                          </tr>
                     </table>
                </asp:Panel>
                <asp:Panel CssClass="popupMenu" ID="PopupMenu" runat="server">
                     <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Update" CausesValidation="true"
                          Text="Update" />
                     <br />
                     <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Cancel" CausesValidation="false"
                          Text="Cancel" />
                           <br />


                </asp:Panel>
                <ajaxToolkit:HoverMenuExtender runat="server" TargetControlID="ItemContainer" PopupControlID="popupMenu"
                     HoverCssClass="popupHover" PopupPosition="Left" OffsetX="0" OffsetY="0" PopDelay="100">
                </ajaxToolkit:HoverMenuExtender>
           </EditItemTemplate>
      </asp:TemplateField>
 </Columns>
   </asp:GridView>
  <asp:SqlDataSource ID="BookingsSQL" runat="server" ConnectionString="<%$  ConnectionStrings:BookingsConnectionString %>"
 OldValuesParameterFormatString="original_{0}" ConflictDetection="OverwriteChanges"
 SelectCommand="SELECT [ID], [ProgrammeName] AS programme_name, [Start], [Finish], [Source], [Destination], [BComment],[Attachment] FROM [DailySheets]"
 DeleteCommand="DELETE FROM [Bookings] WHERE [ID] = @ID" 
 InsertCommand="INSERT INTO [DailySheets] ([programme name], [Start], [Finish], [Source], [Destination], [BComment] , [Attachment]) VALUES (@programme_name, @Start, @Finish, @Source, @Destination, @BComment)"
 UpdateCommand="UPDATE [DailySheets] SET [ProgrammeName] = @programme_name, [Start] = @Start, [Finish] = @Finish, [Source] = @Source, [Destination] = @Destination, [BComment] = @BComment , [Attachment] = @Attachment WHERE [ID] = @ID">
 <DeleteParameters>
      <asp:Parameter Name="ID" Type="Int32" />
      <asp:Parameter Name="Start" Type="DateTime" />
      <asp:Parameter Name="Finish" Type="DateTime" />
      <asp:Parameter Name="Source" Type="String" />
      <asp:Parameter Name="Destination" Type="String" />
      <asp:Parameter Name="BComment" Type="String" />
      <asp:Parameter Name="programme_name" Type="String" />
 </DeleteParameters>
 <InsertParameters>
      <asp:Parameter Name="programme_name" Type="String" />
      <asp:Parameter Name="Start" Type="DateTime" />
      <asp:Parameter Name="Finish" Type="DateTime" />
      <asp:Parameter Name="Source" Type="String" />
      <asp:Parameter Name="Destination" Type="String" />
      <asp:Parameter Name="BComment" Type="String" />
 </InsertParameters>
 <UpdateParameters>
      <asp:Parameter Name="ID" Type="Int32" />
      <asp:Parameter Name="Start" Type="DateTime" />
      <asp:Parameter Name="Finish" Type="DateTime" />
      <asp:Parameter Name="Source" Type="String" />
      <asp:Parameter Name="Destination" Type="String" />
      <asp:Parameter Name="BComment" Type="String" />
      <asp:Parameter Name="programme_name" Type="String" />
 </UpdateParameters>
</asp:SqlDataSource>

3 个答案:

答案 0 :(得分:0)

图像(或任何二进制附件)通常不会像文本内联那样输出到页面。您应该创建一个URL handler来单独处理blob请求并分配正确的内容类型。

这是问题......

<%# Eval("Attachment") %>

感谢@TomTom进行更正!

答案 1 :(得分:0)

尝试使用GridviewRowDataBound事件来确定是否存在从数据库返回的预期值。 将<img>放在TemplateField的{​​{1}}内。 然后根据数据库返回的值,设置图像的URL

答案 2 :(得分:0)

要创建SliverNinja建议的处理程序,您可以执行以下操作。使用以下命令创建ImageHandler.ashx文件:

public class ImageHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string id = context.Request.QueryString["id"];

        if (!string.IsNullOrEmpty(id))
        {
            int programId = 0;

            if (int.TryParse(id, out programId))
            {
                // Get the BLOB image from the database
                var attachment = GetAttachmentImage(programId);

                if (attachment != null)
                {
                    context.Response.ContentType = "image/jpeg";

                    context.Response.BinaryWrite(attachment);
                }
            }
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

然后在你的ASPX页面中:

<img src='ImageHandler.ashx?id=<%# Eval("id") %>' />