0

我正在尝试将 ImageUrl 指向 GridView 的模板字段中的图像,但不断收到错误消息:

你调用的对象是空的。在这条线上:

将 imagePath 调暗为 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
4

2 回答 2

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
于 2011-03-08T13:13:14.217 回答
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"]) %>'
于 2011-03-08T12:53:33.100 回答