Gridview rowdatabound访问数据项vb
我正在尝试将 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
I am trying to an ImageUrl to an image in a Template Field in GridView but keep getting the error:
Object reference not set to an instance of an object. on this line:
Dim imagePath As String = rowView("image_path")
I've never done this before on a GridView but had it working on a ListView.
Thanks for any help heres my code:
.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>
CODE BEHIND
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要检查它是数据行而不是标题行
试试这个
I think you need to check that its a data row and not the header row
Try this
尝试
如果与网格绑定的表具有“image_path”列,则使用更简单的方法...
如果你想构建一些客户字符串
try
If the table binded with the grid has column "image_path", then instead use an easier way....
<asp:Image id="img1" runat="server" imageUrl = '<%#Eval("image_path")%>' />
If you want to build some custome string