簡體   English   中英

將功能綁定到嵌入中繼器的圖像源

[英]bind a function to image source that embedded in a repeater

我有一個中繼器,它內部有圖像。 我想根據排名顯示不同的圖像,例如,如果排名為1,則顯示圖像,如果排名為2,則顯示其他圖像,等等。我也有5種圖像和5級。

排名是數據集中的一列。 但是我的功能無法正常工作,並且無法獲得正確的結果。 它只顯示第一個圖片。 您對此操作建議什么解決方案?

非常感謝 。

這是我的代碼

public string getimg()
{ 
    SqlConnection con = new SqlConnection("data source=.;database=site;integrated security=true;");
     string sSQL = "Select username ,weight,point , Rank() over(order by point desc) as 'ranking' from karbar order by point desc";  
     SqlCommand cmd = new SqlCommand(sSQL, con);
     SqlDataAdapter adapt = new SqlDataAdapter(cmd);
     DataSet ds = new DataSet();
     adapt.Fill(ds);
     foreach (DataRow myRow in ds.Tables[0].Rows)
     {
         if (Convert.ToInt32(myRow["ranking"]) == 1)

         { return "price/con1.png"; }
         else return "price/con2.png";
     }

     }

及其html

  <div class="innerTitle">
                <img style="width:55px;height:55px" alt=""  src="<%# getimg() %>" />     </div>
                  <div class="innerContent" style=" width: 391px; direction:rtl ">

您好,您可以嘗試此答案

1修改您的功能

public string getimg(int indexRow)
{ 
}

2添加此選擇代碼

  if (Convert.ToInt32(myRow["ranking"]) == 1 
         && Convert.ToInt32(myRow["yourIndex"]) == indexRow ) //in order to select nice row
  {  
     return "price/con1.png"; 
  }

  return "price/con2.png";   

3在通話中,您必須在頁面上打印索引行,才能選擇

img style="width:55px;height:55px" alt=""  src="<%# getimg(1) %>" />  //getimg(1) print first row.   

4讓你索引

input type="hidden" runat="server" id="test" value="<%# DataBinder.Eval(Container.DataItem, "YourIndex") />%>" />

嘗試這個:

嘗試使用row_number()函數代替Rank()

Select username ,weight,point , 
 ROW_NUMBER() over(order by point desc) as 'ranking'
 from karbar order by point desc

因為如果兩個用戶具有相同的等級,則他們兩個都將獲得等級為1,而如果使用row_number(),則他們將獲得1和2等級

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM