2013-03-30 26 views
1

它可能無法訪問我其中有一個模板字段它看起來是這樣的一個評論框..'Image1'未被聲明。由於它的權限級別的錯誤

<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'沒有聲明。它可能不可訪問,因爲它的權限級別「在我的代碼後面出錯。智能感知告訴我,有「ProfilePic」未聲明的ID(該DisplayData子例程中的圖像

有問題的代碼位是:

Protected Sub DisplayData() 
    Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString) 
    Dim sql = "SELECT * FROM userprofiles WHERE [email protected]" 
    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 [email protected]" 
    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嗎?

+0

請大肆削減代碼示例問題的最基本方面的變化。我看不到樹木。 – Oded

+0

好吧,我已將代碼切下來。你怎麼看? – adaam

回答

2

要引用的唯一方法是在ProfilePic處將其設置爲DataBind時間。您需要爲您的asp:GridView標記添加OnRowDataBound="GridView2_RowDataBound"來連接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"; 
    } 
} 
+0

非常感謝您的解釋。我理解這個邏輯 - 它非常簡單。不幸的是,我上面的代碼存在問題;它說預計'聲明結束'。抱歉,我自己無法解決這個問題,我只是一個初學者! – adaam

+0

@adaam固定。有一個額外的括號。 –

+0

非常感謝你,我會明天看到這個作品明天當我醒來時 – adaam

1

作爲對照,你可以綁定時只能訪問控制,特別是在OnRowDataBound事件處理程序(對於GridView)。

在這個事件處理程序中,您需要調用FindControl和想要的控件的ID,並將其轉換爲正確的類型(僅當您需要訪問該類型的特定成員時)。

+0

你可能指定一個代碼示例。非其他答案都符合我的要求! – adaam

+1

@adaam - [Lynn](http://stackoverflow.com/users/656243/lynn-crumbling)的[答案](http://stackoverflow.com/a/15722895/1583)實際上就是我所說的。 – Oded

0

試試這個

public string DisplayData() 
    Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("BookMeetConnString").ConnectionString) 
    Dim sql = "SELECT * FROM userprofiles WHERE [email protected]" 
    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" /> 
+0

嗨,這段代碼不適合我。你確定它沒有錯誤嗎?它根本不接受字符串,並且不允許返回。 – adaam

相關問題