'Image1'未声明。由于它的权限级别错误,它可能无法访问

时间:2013-03-30 19:47:21

标签: asp.net vb.net tsql ms-access ado.net

我有一个评论框,其中有一个类似于此的模板字段..

<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”的lookupfield,引用了'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

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

3 个答案:

答案 0 :(得分:2)

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

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

Image

答案 1 :(得分:1)

作为模板中的控件,您只能在绑定时访问该控件,特别是在OnRowDataBound事件处理程序中(对于GridView)。

在此事件处理程序中,您需要使用所需控件的FindControl调用ID并将其强制转换为正确的类型(仅当您需要访问该类型的特定成员时)。

答案 2 :(得分:0)

试试这个

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 

和客户端改变

<asp:Image ID="ProfilePic" **ImageUrl='<%# DisplayData()%>'** runat="server" />