Gridview rowdatabound访问数据项目vb [英] Gridview rowdatabound access data items vb

查看:107
本文介绍了Gridview rowdatabound访问数据项目vb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将ImageUrl映射到GridView中的模板字段中的图像,但不断收到错误消息:



未将对象引用设置为对象的实例。在这一行上:



Dim imagePath As String = rowView(image_path)



我从来没有做过这之前在一个GridView,但它有一个ListView的工作。



感谢您的任何帮助继承我的代码:



.APSX

 < asp:GridView ID =gvImagesDataKeyNames =idrunat =serverAutoGenerateColumns =FalseBorderWidth =0pxGridLines =None> 
<列>
< asp:TemplateField>
< ItemTemplate>
< asp:CheckBox ID =imageIdrunat =server/>
< / ItemTemplate>
< / asp:TemplateField>
< asp:TemplateField>
< ItemTemplate>
< asp:Image ID =imageFilerunat =server>< / asp:Image>
< / ItemTemplate>
< / asp:TemplateField>
< asp:BoundField DataField =id/>
< /列>
< / asp:GridView>

后面的代码

  Protected Sub gvImages_RowDataBound(ByVal sender As Object,ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)处理gvImages.RowDataBound 

Dim rowView As DataRowView = CType(e.Row.DataItem,DataRowView)

Dim imagePath As String = rowView(image_path)

Dim strImageUrl As String =〜/ admin / images /个案/& 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


解决方案

我想你需要检查它的数据行而不是标题行

试试这个

  Protected Sub gvImages_RowDataBound(ByVal sender As Object,ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)处理gvImages.RowDataBound 

如果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


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

解决方案

I think you need to check that its a data row and not the header row

Try this

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

这篇关于Gridview rowdatabound访问数据项目vb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆