2013-10-10 79 views
0

我想獲得列表中某個索引的值如何才能做到這一點?我這裏有一個返回遊戲列表的方法:獲取列表C中某些索引的值

public static List<BO.Game> GetGames() 
    { 
     List<BO.Game> listGame = new List<BO.Game>(); 

     BO.Game game; 



     SqlConnection con = new SqlConnection(); 

     try 
     { 
      con.ConnectionString = connectionString; 
      con.Open(); 

      SqlCommand com = new SqlCommand(); 
      com.Connection = con; 
      com.CommandType = CommandType.StoredProcedure; 
      com.CommandText = "GetDisplayPicOfContest"; 

      SqlDataReader dr = com.ExecuteReader(); 

      while (dr.Read()) 
      { 
       game = new BO.Game(); 
       game.PrizeID = Convert.ToInt32(dr["PrizeID"]); 
       game.Name = dr["LocationName"].ToString(); 
       game.ImageURL = "DisplayImages/" + dr["DisplayImage"].ToString(); 
       game.Country = dr["CountryName"].ToString(); 
       listGame.Add(game); 
      } 


      dr.Close(); 
      dr.Dispose(); 


     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     finally 

     { 
      con.Close(); 
      con.Dispose(); 
     } 

     return listGame; 
    } 

例如,我想顯示的列表

我可以做這樣的事情在第一和第二項的圖片網址?

<div id="mcont2-sub1" class="mcont-subs mcont-subs-2"> 
        <div class="full-detais-cover2"> 

         <a class="opac-link" href="game_details.html" ><img class="hover-img-main" src="<%=listGames[0].ImageURL;%>" /></a> 

         <a class="opac-link" href="game_details.html" ><div id="cover-pager2"><img src="Images/lens-full-details.png" /></div></a> 
         <div class="ticket-quantity2"> 
         <span>Choose ticket quantity</span><span>Tickets $5.00</span> 
          <ul class="quantity-paging"> 
           <li><a>&nbsp;1&nbsp;</a></li> 
           <li><a>&nbsp;2&nbsp;</a></li> 
           <li><a>&nbsp;3&nbsp;</a></li> 
           <li><a>&nbsp;4&nbsp;</a></li> 
           <li><a>&nbsp;5&nbsp;</a></li> 
           <li><a>10</a></li> 
           <li><a>15</a></li> 
           <li><a>20</a></li> 
          </ul> 
         </div> 
        </div> 
        <div class="descrip-place" id="id-descrip-place"> 
         <p class="description">Marbella Beach Club Marriott</p> 
         <p class="place">Spain</p> 
        </div> 
        <a href="game_details.html" class="arrow-light-blue" ><img src="Images/arrow-light-blue.png" /></a> 
       </div> 

       <div id="mcont2-sub2" class="mcont-subs mcont-subs-2"> 
        <a href="game_details.html" ><img src="<%=listGames[1].ImageURL;%>" /></a> 
        <div class="descrip-place" id="Div1"> 
         <p class="description">Waiohai Beach Club Marriott</p> 
         <p class="place">Hawaii</p> 
        </div> 
        <a href="game_details.html" class="arrow-light-blue" ><img src="Images/arrow-light-blue.png" /></a> 
       </div> 
+0

你跟隨MVC? –

+0

你有沒有試過你的代碼? –

+0

@Arun - 這是webforms –

回答

5

我不知道究竟你需要的,但如果你想在列表C#得到一定的指數值此使用square brackets syntax你可以這樣做:

List<something> list = GetListOfSomething(); 
var someItem = list[yourIndex]; 

你可以做到這一點,因爲List<T>實現IList<T>接口定義此索引:

object this[int index] { get; set; } 

你也可以使用Linq to Objects來查詢您的列表。

希望這會有所幫助。

+0

謝謝你的工作! –

0

我認爲最簡單的化解方法是用一個asp.net圖像控制來代替下面的代碼

<img class="hover-img-main" src="<%=listGames[0].ImageURL;%>" /> 

,並從代碼隱藏設置它的SRC。

+0

由於設計問題,我無法用圖像控件替換它。 –

+0

你面臨什麼樣的錯誤?將代碼替換爲以下行,並查看替代文本中用於測試目的的URL類型。 <%=listGames[0].ImageURL;%>「/> –
                        <span class=

+0

以下是錯誤:描述:在編譯此請求所需的資源編譯過程中發生錯誤。請仔細閱讀以下具體錯誤詳細信息並適當修改源代碼。 編譯器錯誤消息:CS1026:)預計 –