带有超链接列的网格视图

本文关键字:网格 视图 超链接 | 更新日期: 2023-09-27 18:36:48

我的页面中有这个GridView

    <asp:GridView ID="GridView2" runat="server" DataSourceID="SqlDataSource2" 
        AllowPaging="True" PageSize="20" CellPadding="4" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" />
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>

这是SqlDataSource

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
                    SelectCommand="SELECT country AS Country,count(country) As Count FROM  client GROUP by country ORDER BY count(country) DESC;" ></asp:SqlDataSource>

我希望这个名字将是超链接到一些像这样:

/Details.aspx?country=countryname

我该如何实现它?

带有超链接列的网格视图

使用包含 HyperLink 控件的<TemplateField>,如下所示:

<asp:GridView ...>
    <Columns>
        <asp:TemplateField HeaderText="">
            <ItemTemplate>
                <asp:HyperLink runat="server" ID="HyperLink1" 
                    NavigateUrl='<%# "Details.aspx?country=" + Eval("Country")%>' 
                    Text='Details'>
                </asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

           <asp:HyperLink runat="server" ID="HyperLink1" 
               NavigateUrl='<%# "/Details.aspx?country=" + Eval("Country")%>' 
               Text='Details'>
           </asp:HyperLink>