【发布时间】:2018-01-08 20:07:48
【问题描述】:
我试图在我的 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
最好的方法是什么?
【问题讨论】: