如何在gridview(asp.net)中添加链接?

时间:2012-01-14 01:07:58

标签: asp.net sql vb.net ado.net

我在asp.net vb中创建了一个gridview。我想在id列上添加一个链接,我想创建一个可以登陆新页面的链接等http://localhost/defualt.aspx?id=(来自datarow的id行)。是否可以使用gridview?代码如下

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="EmployeeID" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" 
            InsertVisible="False" ReadOnly="True" SortExpression="EmployeeID" />
        <asp:BoundField DataField="LastName" HeaderText="LastName" 
            SortExpression="LastName" />
        <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
            SortExpression="FirstName" />
    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:NwindConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:NwindConnectionString.ProviderName %>" 
    SelectCommand="SELECT [EmployeeID], [LastName], [FirstName] FROM [Employees]">
</asp:SqlDataSource>

2 个答案:

答案 0 :(得分:2)

您可以使用HyperLinkField

     <asp:HyperLinkField DataNavigateUrlFields="EmployeeID" 
DataNavigateUrlFormatString="/default.aspx?id={0}"
                            DataTextField="EmployeeID" HeaderText="EmployeeID" />

答案 1 :(得分:0)

删除EmployeeID的BoundField并替换TemplateField。试试这个:

<asp:TemplateField HeaderText="EmployeeID" InsertVisible="False" 
                ShowHeader="False" SortExpression="EmployeeID">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" 
                        CommandName="" PostBackUrl='<%# "http://localhost/defualt.aspx?id=" & Eval("EmployeeID") %>' Text='<%# Eval("EmployeeID") %>'></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>