Linked Questions

Popular Questions

Displaying static hyperlink in grid view

Asked by At

I am getting the data from the SQL DB from the column name Nature, Hyper link should be decided based on the value of Nature.

If the value of nature is

  1. A or B or C : Link should be ""
  2. X or Y or Z : Link should be ""
  3. P or Q or R : Link should be ""

Output on the page should be :

Nature Link(Column name on page)

If value is A -----> A (When Clicked on 'A' link should open )

If value is Q -----> Q (When Clicked on 'Q' link should open )

I am not able to display the Value (A,B,C,P,Q....) and associated link for that.


1. Using HyperlinkField in Gridview

<asp:HyperlinkField HeaderText="Nature Link" DataTextField="Nature"
    Visible="true" SortExpression="Nature"
    DataNavigateUrlFields="Nature" target="_blank"></asp:HyperlinkField>

Code :

protected void dginvoicereport_RowDataBound(object sender, GridViewRowEventArgs e)
    if (e.Row.RowType != DataControlRowType.Header)
        var t_nature= Int32.Parse(e.Row.Cells[25].Text.Replace("&nbsp;",""));
        if ((t_nature== 'A') || (t_nature== 'B') || (t_nature== 'C'))
            e.Row.Cells[25].Text = "test1";
        else if ((t_nature== 'X') || (t_nature== 'Y') || (t_nature== 'Z'))
            e.Row.Cells[25].Text = "test2";

2. This:

<asp:BoundField DataField="nature" HeaderText="Nature Link"
    HtmlEncode="False" DataFormatString="<a target='_blank'
    href='{0}'>Link</a>" />    `

Related Questions