我正在尝试将ImageUrl映射到GridView中的模板字段中的图像,但不断收到错误:
对象引用未设置为对象的实例。在这一行:
Dim imagePath As String = rowView(" image_path")
我以前从未在GridView上做过这个,但是它在ListView上工作。
感谢您提供任何帮助我的代码:
.APSX
<asp:GridView ID="gvImages" DataKeyNames="id" runat="server" AutoGenerateColumns="False" BorderWidth="0px" GridLines="None">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="imageId" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="imageFile" runat="server"></asp:Image>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="id" />
</Columns>
</asp:GridView>
代码背后
Protected Sub gvImages_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvImages.RowDataBound
Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)
Dim imagePath As String = rowView("image_path")
Dim strImageUrl As String = "~/admin/images/cases/" & Request.QueryString("uid") & "/" & imagePath
Dim imageFile As System.Web.UI.WebControls.Image = CType(e.Row.FindControl("imageFile"), System.Web.UI.WebControls.Image)
imageFile.ImageUrl = strImageUrl
End Sub
答案 0 :(得分:3)
我认为你需要检查它是一个数据行而不是标题行
试试这个
Protected Sub gvImages_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvImages.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim rowView As DataRowView = CType(e.Row.DataItem, DataRowView)
Dim imagePath As String = rowView("image_path")
Dim strImageUrl As String = "~/admin/images/cases/" & Request.QueryString("uid") & "/" & imagePath
Dim imageFile As System.Web.UI.WebControls.Image = CType(e.Row.FindControl("imageFile"), System.Web.UI.WebControls.Image)
imageFile.ImageUrl = strImageUrl
End If
End Sub
答案 1 :(得分:0)
试
Dim imagePath As String = e.rowView("image_path").ToString()
如果与网格绑定的表格中包含“image_path”列,那么请使用更简单的方法....
<强> <asp:Image id="img1" runat="server" imageUrl = '<%#Eval("image_path")%>' />
强>
如果你想建立一些客户字符串
imageurl = '<%# String.Format("{0} - {1} - {2} - {3}", Eval("Name1"), Eval("Name2"), Session["abc"],Request["abc"]) %>'