2010-04-25 84 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>