I have created the dummy table which contains the data as you have mention. On Button Click event i am checking the value of Type in Gridview. If its External then i am redirecting it to the website which is mention in the Address column of the GridView
HTML:
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvLinks" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:BoundField DataField="Type" HeaderText="Type" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnLink" runat="server" Text="View" OnClick="OpenExternalLink" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.PopulateGrid();
}
}
private void PopulateGrid()
{
DataTable dt2 = new DataTable();
dt2.Columns.AddRange(new DataColumn[3]{new DataColumn("ID",typeof(int)),
new DataColumn("Address",typeof(string)),
new DataColumn("Type",typeof(string))
});
dt2.Rows.Add(1, "Abc", "Fixed");
dt2.Rows.Add(2, "http://www.google.com", "External");
dt2.Rows.Add(3, "Xyz", "Fixed");
dt2.Rows.Add(4, "Pqo", "Fixed");
dt2.Rows.Add(5, "http://www.w3schools.com", "External");
dt2.Rows.Add(6, "http://www.aspforums.com", "External");
dt2.Rows.Add(7, "Abc", "Fixed");
dt2.Rows.Add(8, "Abc", "Fixed");
dt2.Rows.Add(9, "http://www.aspsnippets.com", "External");
dt2.Rows.Add(10, "Abc", "Fixed");
this.gvLinks.DataSource = dt2;
this.gvLinks.DataBind();
}
protected void OpenExternalLink(object sender, EventArgs e)
{
GridViewRow row = ((sender as Button).NamingContainer as GridViewRow);
if (row.Cells[2].Text == "External")
{
Response.Redirect(row.Cells[1].Text);
}
}
Hope its according to your problem
Thanks..