我试图在我的ASP Gridview中显示图片,但图片并不像其他数据那样来自数据库。它是根据数据库中的字段生成的。当我尝试在gridview列中运行一个函数时,它会给我一个错误,即在此上下文中不支持代码块,因此我试图找出实现此目的的最佳方法是什么?
<asp:GridView ID="g1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" PageSize="25"
Width="100%" Font-Size="Medium" DataKeyNames="InternalReference">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:ImageField DataImageUrlField="<% GetPicture1(Session("DealerID"), Eval("Uid")) %>"></asp:ImageField>
<asp:CommandField ShowSelectButton="True" HeaderText="View" SelectText="View" />
<asp:BoundField DataField="InternalReference" HeaderText="InternalReference"
SortExpression="InternalReference" Visible="False" />
<asp:BoundField DataField="VehicleYear" HeaderText="Year"
SortExpression="VehicleYear" />
<asp:BoundField DataField="VehicleMake" HeaderText="Make"
SortExpression="VehicleMake" />
<asp:BoundField DataField="VehicleModel" HeaderText="Model"
SortExpression="VehicleModel" />
<asp:BoundField DataField="Mileage" HeaderText="Mileage"
SortExpression="Mileage" ReadOnly="True"
NullDisplayText="Call For Details" DataFormatString="{0:###,###,##0}" />
<asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price"
DataFormatString="{0:c}" ReadOnly="True" NullDisplayText="Call For Price" >
</asp:BoundField>
</Columns>
<HeaderStyle BackColor="#1F1F1F" ForeColor="White" />
<RowStyle Height="30px" />
</asp:GridView>
第一列是我试图插入图片的地方。
<asp:ImageField DataImageUrlField="<% GetPicture(Session("DealerID"), Eval("Uid")) %>"></asp:ImageField>
在GetPicture()函数内部,它接收经销商ID,然后车辆ID生成图像的URL。
Public Function GetPicture(ByVal sDealerReference As String, ByVal sReference As String) As String
If IO.File.Exists("Z:\FTP Customers\CarPhotos\" & Session("DealerID") & "\" & Trim(sReference) & ".0.jpg") Then
Return "http://www.acarforless.com/carimages/" & Session("DealerID") & "/" & Trim(sReference) & ".0.jpg"
Else
Return "http://www.acarforless.com/carimages/nopicture.jpg"
End If
End Function
我能做到这一点的最好方法是什么?
答案 0 :(得分:1)
您可以使用带有Image控件的TemplateField代替ImageField,因为Image控件允许您为ImageUrl指定数据绑定表达式,例如
<asp:TemplateField>
<ItemTemplate>
<asp:Image runat="server" ImageUrl='<%# GetPicture(Session("DealerID"), Eval("Uid")) %>' />
</ItemTemplate>
</asp:TemplateField>