【问题标题】:Gridview rowdatabound access data items vbGridview rowdatabound 访问数据项 vb
【发布时间】:2011-03-08 12:07:53
【问题描述】:

我正在尝试将 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

【问题讨论】:

  • 你确定 rowView("image_path") 没有返回 DBNULL。
  • 它包含一个图像的名称,所以为了仔细检查我刚刚删除了图像模板字段并将 image_path 添加到数据绑定字段并显示图像名称。所以这不可能。

标签: asp.net vb.net gridview rowdatabound


【解决方案1】:

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

试试这个

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

【讨论】:

  • 啊,谢谢 Nicky,已经整理好了,我在搜索时看到了类似的东西,但没有意识到这是必须的。谢谢。 J.
  • 过去我被这个绊倒了很多次,所以没问题。
【解决方案2】:

试试

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

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

&lt;asp:Image id="img1" runat="server" imageUrl = '&lt;%#Eval("image_path")%&gt;' /&gt;

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

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

【讨论】:

  • 不,也不工作,我收到一个智能感知错误,说 rowView 不是...的成员,但如果我取下 e.智能感知错误消失了,但我在浏览器中的这一行仍然遇到同样的错误。
  • 嗨 AmRan,是的,我的工作正常,但我正在学习 .net,并且想知道如何从后面的代码中做到这一点,因为我确信我将来必须这样做.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多