English 中文(简体)
ASP.NET-网格视图不显示图像
原标题:ASP.NET- Gridview not display image
<form id="form1" runat="server">
<div>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="bookname"  Height="504px" 
        Width="289px">
        <Columns>
            <asp:TemplateField HeaderText="image">
                <ItemTemplate>
                    <asp:Image ID="Image1" runat="server" 
                        ImageUrl= <%# String.Format("~/path/to/image/" + Eval("image")) %>  />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text= <%# Eval("image") %> ></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="bookname" HeaderText="bookname" ReadOnly="True" 
                SortExpression="bookname" />
            <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        SelectCommand="SELECT [image], [bookname], [price] FROM [books]">
    </asp:SqlDataSource>

My Image field is saved in (SQL server 2005)...and image inserted in database through file upload button ... and now i want to show data in gridview but not display image field and other field shown in gridview very well becauese other fields are text format.

问题回答
<asp:Image ID="Image1" runat="server" ImageUrl= <%# String.Format("~/path/to/image/{0}" , Eval("image")) %>  />   

您的问题在于 < code> stringing. Format 接受参数, 您正在使用一个 concat 操作 。 这里我使用了 < code_ 0} - 表示在/ index 0 处的参数。 对于多个参数, 您也可以使用 < code> string. Format ("{0} {1} {2} {2}, var1, var2, var3] 。

上面我还假设 {0} 将被扩展名的文件名替换, 如 abc. png 。

<form id="form1" runat="server">
    <div>
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="bookname"  Height="504px" 
            Width="289px">
            <Columns>
                <asp:TemplateField HeaderText="image">
                    <ItemTemplate>
                        <asp:Image ID="Image1" runat="server" 
                            ImageUrl= <%# String.Format("~/path/to/image/{0}" , Eval("image")) %>  />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text= <%# Eval("image") %> ></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="bookname" HeaderText="bookname" ReadOnly="True" 
                    SortExpression="bookname" />
                <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            SelectCommand="SELECT [image], [bookname], [price] FROM [books]">
        </asp:SqlDataSource>

我会期待 某样东西,比如

ImageUrl= <%# this.ResolveUrl("~/path/to/image/" + Eval("image")) %>  />

而不是string. format

您的图像存储在数据库中, 您需要使用处理器来检索图像 。 您可以在这里获得一个参考文件 < a href=" http://www.codeproject. com/ artes/ 271590/ show- an- image- saveed- in- a- SQL- Table- on- an- ASP- Net- I" rel= “ nofollow > http://www.codeproject. com/ arts/ 271590/ show- an- image- saved- int- a- SQL- Table- on- an- ASP- Net- I < /a >





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签