Here i am binding the Data with dummy table and i have a View Button inside the Gridview and if Gridview column Type contains "External" then it will redirect to that website and if its "Fixed" then JQuery Popup will open with the Message Containg the Address.
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" OnClientClick="OpenDialog();"
OnClick="OpenExternalLink" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<div id="dialog" style="display: none">
</div>
</div>
</form>
SCRIPT:
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript">
function ShowPopup(message) {
$(function () {
$("#dialog").html(message);
$("#dialog").dialog({
title: "jQuery Dialog Popup",
buttons: {
Close: function () {
$(this).dialog('close');
}
},
modal: true
});
});
};
</script>
</head>
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);
}
else
{
string message = row.Cells[1].Text;
ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + message + "');", true);
}
}
Thank you.