Gridview rowdatabound访问数据项vb

发布于 2024-10-20 23:42:11 字数 1611 浏览 3 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

杯别 2024-10-27 23:42:11

我认为您需要检查它是数据行而不是标题行

试试这个

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

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
心碎的声音 2024-10-27 23:42:11

尝试

Dim imagePath As String = e.rowView("image_path").ToString()

如果与网格绑定的表具有“image_path”列,则使用更简单的方法...

如果你想构建一些客户字符串

imageurl = '<%# String.Format("{0} - {1} - {2} - {3}", Eval("Name1"), Eval("Name2"), Session["abc"],Request["abc"]) %>'

try

Dim imagePath As String = e.rowView("image_path").ToString()

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

imageurl = '<%# String.Format("{0} - {1} - {2} - {3}", Eval("Name1"), Eval("Name2"), Session["abc"],Request["abc"]) %>'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文