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 "http://www.this.is.test1"
  2. X or Y or Z : Link should be "http://www.this.is.test2"
  3. P or Q or R : Link should be "http://www.this.is.test3"

Output on the page should be :

Nature Link(Column name on page)

If value is A -----> A (When Clicked on 'A' link http://www.this.is.test1 should open )

If value is Q -----> Q (When Clicked on 'Q' link http://www.this.is.test3 should open )

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

Tried

1. Using HyperlinkField in Gridview

<asp:HyperlinkField HeaderText="Nature Link" DataTextField="Nature"
    Visible="true" SortExpression="Nature"
    DataNavigateUrlFormatString="http://www.this.is.{0}"
    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='http://www.this.is.{0}'>Link</a>" />    `

Related Questions