0

I have the following code which creates a list with pagination enabled:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="true"
        emptydatatext="No data available." 
        allowpaging="true" 
        runat="server">

        <pagersettings mode="Numeric"
        position="Bottom"           
        pagebuttoncount="10"/>

        <pagerstyle backcolor="LightBlue"
        height="30px"
        verticalalign="Bottom"
        horizontalalign="Left"/>

        </asp:gridview>

        <asp:sqldatasource id="CustomersSource"
        selectcommand="select id, text from table1"
        connectionstring="connection string here" 
        runat="server"/>

    </div>
    </form>
</body>
</html>

Is it possible to hide the id, and convert the text into a link while at the same time applying the id to the link?

i.e. if the first row from the database from the text column contains "document one" and the id is 1, at the moment, it will display id as 1 and text as "document one" in the gridview. how do I change that to something like <a href="http://mysite/document.aspx?id=1">document one</a>?

oshirowanen
  • 15,297
  • 82
  • 198
  • 350

2 Answers2

2

Instead of setting AutoGenerateColumns = True you need to specify the columns and use a HyperlinkField.

Look at the last example in this article where they use the datanavigateurlfields and datanavigateurlformatstring attributes to generate a link with parameters from the DataSource.

granaker
  • 1,318
  • 8
  • 13
0

Try using the RowDataBound event to format your columns. This gives you great control on what goes into each cell. You can create empty controls (a simple hyperlink) in the ItemTemplate and then do the appropriate data binding/formatting in the RowDataBound event.

It seems a little daunting at first, but once you get the hang of it, it will give you a lot of room in which to easily customize your GridView output.

Dillie-O
  • 29,277
  • 14
  • 101
  • 140