2012-01-14 162 views
1

我在asp.net vb中做了一個gridview。並且我想添加一個id列的鏈接,我想創建一個可以登陸到新頁面的鏈接等。http://localhost/defualt.aspx?id=(idar row from datarow)。用gridview可以嗎?代碼如下如何在gridview(asp.net)中添加鏈接?

<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

您可以使用HyperLinkField字段

 <asp:HyperLinkField DataNavigateUrlFields="EmployeeID" 
DataNavigateUrlFormatString="/default.aspx?id={0}" 
          DataTextField="EmployeeID" HeaderText="EmployeeID" /> 
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>