1

我有一个评论框,其中有一个看起来像这样的模板字段..

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="CommentsDataSource" Height="167px" Width="325px">
    <Columns>
        <asp:TemplateField HeaderText="Comments">
            <ItemTemplate>
                <div style="background-color:Silver">
                    <div class="avatar-frame">
                        <asp:Image ID="ProfilePic" runat="server"/>
                    </div>
                    <h1><%# Eval("TagLine")%></h1>
                    <h2><%# Eval("IfNonMemberUserName")%></h2>
                    <p><%# Eval("CommentBody")%></p>
                 </div>
            </ItemTemplate>
            <AlternatingItemTemplate>
                <div style="background-color:White">
                    <div class="avatar-frame">
                    </div>
                    <h1><%# Eval("TagLine")%></h1>
                    <h2><%# Eval("IfNonMemberUserName")%></h2>
                    <p><%# Eval("CommentBody")%></p>
                </div>
            </AlternatingItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="CommentsDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:BookMeetConnString %>" ProviderName="<%$ ConnectionStrings:BookMeetConnString.ProviderName %>"  SelectCommand="SELECT [IfNonMemberUserName], [UserAvatar], [TagLine], [CommentBody] FROM [comments] WHERE ([BookID] = ?)">
    <SelectParameters>
        <asp:QueryStringParameter Name="?" QueryStringField="ID" />
    </SelectParameters>
</asp:SqlDataSource>

一些背景知识: 我有一个 MS Access 数据库,其中有一个名为“userprofiles”的表,其中有一个名为 AvatarURL 的字段。类似地,还有一个名为“comments”的表,其中有一个名为“UserAvatar”的查找字段,指的是“userprofiles”表的“AvatarURL”字段。

我在后面的代码中收到“'ProfilePic' 未声明。由于它的权限级别,它可能无法访问”错误。Intellisense 告诉我 ID 为“ProfilePic”的图像未声明(在 DisplayData 子例程中。

有问题的代码是:

Protected Sub DisplayData()
    Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
    Dim sql = "SELECT * FROM userprofiles WHERE TravellerName=@f1"
    Dim cmd = New OleDbCommand(sql, conn)
    cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
    conn.Open()
    Dim profileDr = cmd.ExecuteReader()
    profileDr.Read()
    If Not IsDBNull(profileDr("AvatarURL")) Then ProfilePic.ImageUrl = profileDr.Item("AvatarURL")
    conn.Close()
End Sub 

在运行时,detail.aspx 工作正常,但评论框中的头像根本不显示。我究竟做错了什么?

编辑

我设法做到了这一点:

    Protected Sub GridView2_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
    Dim sql = "SELECT * FROM userprofiles WHERE TravellerName=@f1"
    Dim cmd = New OleDbCommand(sql, conn)
    cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
    conn.Open()
    Dim profileDr = cmd.ExecuteReader()
    profileDr.Read()
    Dim ProfilePic
    If e.Row.RowType = DataControlRowType.DataRow Then
        ProfilePic = e.Row.FindControl("ProfilePic")
        If Not IsDBNull(profileDr("AvatarURL")) Then ProfilePic.ImageUrl = profileDr.Item("AvatarURL")
    End If
    conn.Close()

End Sub

但是,图像仍然不会在运行时出现。这有什么问题?我应该使用数据阅读器吗?

4

3 回答 3

2

引用 get at 的唯一方法ProfilePic是在 DataBind 时设置它。您需要通过添加OnRowDataBound="GridView2_RowDataBound"到您的asp:GridView标签来连接 GridView2_RowDataBound 事件。

然后,您将获取RowDataBound每一行的事件(甚至是页眉和页脚行,因此您需要测试当前触发事件的行类型。然后您可以FindControl在当前行项目上使用来查找当前行的ProfilePic. ' 必须将该函数的输出转换为Image.

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Image ProfilePic = (Image)e.Row.FindControl("ProfilePic");
        ProfilePic.ImageUrl = "stuff";
    }
}
于 2013-03-30T19:59:40.503 回答
1

As a control within a template, you can only access the control when binding, in particular in the OnRowDataBound event handler (for a GridView).

In this event handler you need to call FindControl with the ID of the wanted control and cast it to the right type (only if you need to access specific members of that type).

于 2013-03-30T19:59:54.677 回答
0

Try this

public string DisplayData()
    Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString)
    Dim sql = "SELECT * FROM userprofiles WHERE TravellerName=@f1"
    Dim cmd = New OleDbCommand(sql, conn)
    cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
    conn.Open()
    Dim profileDr = cmd.ExecuteReader()
    profileDr.Read() 
  string imagename= profileDr("AvatarURL")   
    conn.Close()
return imagename

End Sub 

and client side change for

<asp:Image ID="ProfilePic" **ImageUrl='<%# DisplayData()%>'** runat="server" />
于 2013-03-30T20:27:26.647 回答