2014-02-07 37 views
0

好的,所以我有了這個網站,並且我已經在代碼中聲明瞭幾個數組。我試圖在aspx頁面上使用它們,但它告訴我,一旦深入到二級div元素,他們無法訪問。見代碼:由於保護級別,無法訪問頁面字段

後面的代碼:

string[] strFilePath = new string[5]; 
string[] strTitle = new string[5]; 
string[] strCity = new string[5]; 
string[] strCountry = new string[5]; 


protected void Page_Load(object sender, EventArgs e) 
{ 
    string constr = 
    @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\TravelJoansDB.mdb;"; 
    string cmdstr = "SELECT TOP 5 * FROM Table2 ORDER BY TravelDate"; 
    OleDbConnection con = new OleDbConnection(constr); 
    OleDbCommand com = new OleDbCommand(cmdstr, con); 

    con.Open(); 
    OleDbDataReader reader = com.ExecuteReader(); 
    for (int i = 0; i < 5; i++) 
    { 
     reader.Read(); 
     strFilePath[i] = reader[2].ToString(); 
     strTitle[i] = reader[6].ToString(); 
     strCity[i] = reader[7].ToString(); 
     strCountry[i] = reader[8].ToString(); 
    } 
} 

而這裏的ASP:

<a href="BlogPosts.aspx" id="iview"> 
    <!-- Slide 1 --> 
    <div data-iview:image="placeImages/" + <%= strFilePath[0] %>> 
    <!-- Caption 1 --> 
     <div class="iview-caption" data-x="400" data-y="400" 
       data-transition="wipeRight" data-speed="700"> 
      <h3><%= strTitle[0] %></h3> 
      <%= strCity[0] %>, <%= strCountry[0] %> 
     </div> 
    </div> 

所以它告訴我,strTitlestrCitystrCountry無法訪問由於其保護級別,但strFilePath是涼。我有一種感覺,那是因爲div元素的類屬性。如果是這樣,是否有辦法解決這個問題?我嘗試使用「這個」,但那也沒用。

+0

div的CSS類對代碼後面的變量範圍沒有影響。 – mason

+0

很高興知道。對我的問題有任何想法? – Joseph

+0

你不能在ASPX頁面''data-iview:image =「placeImages /」+ <%= strFilePath [0]%>'上連接這樣的字符串。 – mason

回答

2

需要使變量受保護,以便可以在頁面中看到它們。它們默認是私有的。 strFilePath也不酷,但可能會被忽略。

protected string[] strFilePath = new string[5]; 
protected string[] strTitle = new string[5]; 
protected string[] strCity = new string[5]; 
protected string[] strCountry = new string[5]; 
+1

+1。在使用答案時,除非您的本地編碼準則要求使用[匈牙利符號](http://en.wikipedia.org/wiki/Hungarian_notation),否則請考慮刪除'str'前綴。 –

+0

好價格。謝謝@dbugger。現在我有一個新問題,如果我無法及時弄清楚,我會另外提出一個問題。謝謝! – Joseph

相關問題