8

I am trying to create a webpage that has a gridview. this gridview is supposed to have a link like below

http://localhost/Test.aspx?code=123

when the user clicks one of the rows' link in gridview, it will open a blank page and display some result.

here is how I bind data to the gridview but I dont know how to set the link

protected void Page_Load(object sender, EventArgs e)
{
    string firma_no = logoFrmNr.ToString().PadLeft(3, '0');
    string active_period = logoFrmPeriod.PadLeft(2, '0');

    SqlConnection conn = new SqlConnection(conStr);
    string selectSql = @"SELECT 
                                LOGICALREF,
                                CODE , 
                                DEFINITION_ ,
                                FROM 
                                LG_CLFLINE";

    SqlCommand cmd = new SqlCommand(selectSql, conn);
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dt);
    GridView1.DataSource = dt;
    GridView1.DataBind();

    conn.Close();
}

here is the markup

<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True">
</asp:GridView>

How can I make a link out of CODE column?

Arif YILMAZ
  • 5,210
  • 24
  • 87
  • 174

4 Answers4

9

Add this to your Columns definition in the markup for your grid view:

<asp:TemplateField HeaderText="Hyperlink">
    <ItemTemplate>
        <asp:HyperLink ID="HyperLink1" runat="server" 
            NavigateUrl='<%# Eval("CODE", @"http://localhost/Test.aspx?code={0}") %>' 
            Text='link to code'>
        </asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>
Karl Anderson
  • 33,426
  • 12
  • 62
  • 78
8

There's a trick to this. The Hyperlinkcolumn won't work, because you can't format the link. You want to use a boundfield and format the text. Like so

<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True">
    <Columns>
        <asp:BoundField DataField="Code" HtmlEncode="False" DataFormatString="<a target='_blank' href='Test.aspx?code={0}'>Link Text Goes here</a>" />
    </Columns>
</asp:GridView>

Alternately, you can use a templatefield if you need to designate edit and insert templates.

Karl Anderson
  • 33,426
  • 12
  • 62
  • 78
Smeegs
  • 8,796
  • 5
  • 34
  • 71
  • where should I add this code? please tell me where I should put this code? – Arif YILMAZ Nov 05 '13 at 20:59
  • @Smeegs - you can use a `HyperLink` control inside of a `TemplateField` and format the URL for navigation, see my answer. – Karl Anderson Nov 05 '13 at 21:01
  • I know, I mention that in my answer. But, unless you're handling an edit and an insert template. That's overkill to get the same result. – Smeegs Nov 05 '13 at 21:03
  • @Smeegs - completely disagree that a `TemplateField` is overkill unless you are doing an edit or an insert template, it is extremely useful for handling the data binding event (`OnRowBound`) so that you can find the control by ID instead of relying on cell index, because `BoundField`s do not have an `ID` attribute. – Karl Anderson Nov 05 '13 at 21:07
  • @KarlAnderson It really depends on how you want to use the column. I completely agree with your example. In that scenario `TemplateField` is not overkill, it's completely necessary. But, in this scenario, the poster just wants to render a hyperlink. For the requested functionality creating an entire template compared to formatting a string is overkill. – Smeegs Nov 05 '13 at 21:16
  • @Smeegs - +1 agreed, as long as the OP got the answer they wanted, that is what matters. :-) – Karl Anderson Nov 05 '13 at 21:23
  • @KarlAnderson Cheers :) – Smeegs Nov 05 '13 at 21:24
  • I am confused about the link. I have the same issue and I do not know what link should I place. Please help me – Georgios May 28 '16 at 10:51
  • Here is how I got just the link text only in gridview, which has dynamic text that can't be hard coded as the example above shows `` – Bbb Oct 10 '18 at 18:44
3

to me it would be something like

<asp:DataGrid id="MyDataGrid" 
           GridLines="Both"
           AutoGenerateColumns="false"
           runat="server">

         <HeaderStyle BackColor="#aaaadd"/>

         <Columns>

            <asp:HyperLinkColumn
                 HeaderText="Select an Item"
                 DataNavigateUrlField="code"
                 DataNavigateUrlFormatString="http://localhost/Test.aspx?code={0}"
                 Target="_blank"/>

         </Columns>

      </asp:DataGrid>
pedrommuller
  • 14,865
  • 9
  • 65
  • 115
  • Surprisingly, the `DataNavigateUrlFormatString` doesn't work like that. I wish it did, but it's used to format numerical values. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.hyperlinkfield.datanavigateurlformatstring(v=vs.110).aspx – Smeegs Nov 05 '13 at 21:13
  • My mistake. I was thinking of something else. – Smeegs Nov 05 '13 at 23:40
2

You should be able to use a HyperLinkColumn in your markup.

HyperlinkColumn Documentation

Tim
  • 3,929
  • 7
  • 33
  • 59