2010-04-25 45 views
1

我有一個gridview和一列gridview正在顯示其路徑存儲在數據庫中的圖像&圖像存儲在我的網站內的本地文件夾。我的問題是我想使用圖像的超鏈接控件,以便當圖像被點擊時,它應該跳轉到另一頁。我怎樣才能做到這一點?在gridview中使用超級鏈接控件與imagefield控件

回答

1

首先,你應該將數據綁定到您的網格(在代碼後面):

public override void DataBind() 
{ 
    // you implementation of getting data 
    yourGridId.DataSource = GetData(); 
    yourGridId.DataBind(); 
} 

然後我會建議使用模板領域你的形象:

<asp:gridview id="yourGridId" 
    runat="server"> 
    <columns> 
     <asp:templatefield headertext="An Image"> 
      <itemtemplate> 
       <a href="pageWhereToGo.aspx"> 
        <img src='<%# ResolveUrl((string)Eval("ImageUrl"))%>' /> 
       </a>    
      </itemtemplate> 
     </asp:templatefield> 
    </columns> 
    </asp:gridview> 

代碼上面假設數據庫中映像的路徑存儲爲應用程序的相對路徑(例如~/assets/images/image1.jpg)或完整路徑(例如http://www.contoso.com/assets/images/image1.jpg)。另外假設您的數據源在ImageUrl字段中包含圖像路徑。

所以上面的例子是一個最簡單的網格,有一個asp:templatefield列:這裏是一個可點擊的圖片,點擊事件後,點擊pageWhereToGo.aspx頁面。

有關Gridview列字段的更多信息,請參閱here

0

而不是使用數據綁定字段,你也可以在GridView控件中使用TemplateFiled:

<asp:TemplateField HeaderText="SomeText" > 
    <ItemTemplate> 
     // Put any kind of .NET Code in here 
     // you can access the data bound values like this: 
     <%# Eval("NameOfPropertyOnDataBoundObject")%> 
    </ItemTemplate> 
    <ItemStyle CssClass="tworows"></ItemStyle> 
</asp:TemplateField>